简体   繁体   English

通过代理时连接超时

[英]Connection timed out when going through proxy

This code works perfectly when not going through a proxy server. 当不通过代理服务器时,此代码可以完美地工作。 it loads the required webpage. 它会加载所需的网页。 When i uncomment one line I get an error. 当我取消注释一行时,出现错误。

Error message 错误信息

 "The underlying connection was closed: The connection was closed unexpectedly." System.Exception System.Net.WebException 

The one line that causes the error. 导致错误的一行。

//request.Proxy = new Webproxy("192.157.252.245",80);

The proxy I am using works fine when going through a proxy website or setting it as local proxy. 通过代理网站或将其设置为本地代理时,我使用的代理工作正常。

Uri address = new Uri("http://google.com/");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";

////////Add Proxy
//request.Proxy = new Webproxy("192.157.252.245",80);
////////End Add Proxy

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader streamReader = new StreamReader(response.GetResponseStream());
    string strReaderXML = streamReader.ReadToEnd();
}

First of all do one layman check, because sometime IP is not reachable or port is not open to receive connection, may be firewall don't allow your connection. 首先做一个门外汉检查,因为某个IP是无法到达或端口未打开收到的连接,可能是防火墙不允许的连接。

Kindly visit below links, which could help you to solve your problem: 请访问以下链接,这些链接可以帮助您解决问题:

  1. System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly System.Net.WebException:基础连接已经关闭:连接被意外关闭
  2. The underlying connection was closed: The connection was closed unexpectedly 基础连接已关闭:连接被意外关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM