【问题标题】:HttpWebRequest cannot connect through proxy?HttpWebRequest 无法通过代理连接?
【发布时间】:2013-12-02 20:04:17
【问题描述】:

根据我所阅读的所有内容,我正在尝试实现一些应该很简单的事情,但对我不起作用:通过代理发送任何请求。

请看下面的代码;只要将 2 行注释掉,它就可以工作。一旦我把它们放回去并尝试使用任何代理,请求就会不断超时,抛出“无法连接到远程服务器”WebException,内部异常消息“连接尝试失败,因为连接方在一段时间后没有正确响应时间,或建立连接失败,因为连接的主机未能响应 xxx.xxx.xxx.xxx:zzzz”。

http://www.ip-adress.com/Proxy_Checker/ 用于获取测试代理列表。

var request = (HttpWebRequest) WebRequest.Create("http://google.com/");
//var myproxy = new WebProxy("http://xxx.xxx.xxx.xxx:zzzz", false);
//request.Proxy = myproxy;
request.Method = "GET";
var response = (HttpWebResponse) request.GetResponse();

我显然遗漏了一些东西,我发现的所有类似问题要么有更复杂的问题,要么没有得到解答。

谢谢。

【问题讨论】:

  • 在浏览器中配置代理地址是否有效?
  • 您的代码有效,请尝试使用透明类型的代理。

标签: c# .net proxy httpwebrequest


【解决方案1】:
Uri address = new Uri("http://google.com/");               
// Create the web request 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.Proxy = new WebProxy("ProxyIP", "Port");
request.Proxy.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword");

// Write data  
using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
}

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

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2018-06-03
    相关资源
    最近更新 更多