通过代理

时间:2018-10-24 23:06:11

标签: c# networking proxy network-programming httpwebrequest

当不通过代理服务器时,此代码可以完美地工作。它会加载所需的网页。当我取消注释一行时,我得到一个错误。

  

错误消息

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

导致错误的一行。

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

我正在使用的代理通过代理网站或将其设置为本地代理时工作正常。

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();
}

1 个答案:

答案 0 :(得分:0)

首先进行一次外行检查,因为有时IP无法访问或端口未打开以接收连接,则可能是防火墙不允许您进行连接。

请访问以下链接,这些链接可以帮助您解决问题:

  1. System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly
  2. The underlying connection was closed: The connection was closed unexpectedly