简体   繁体   English

HttpWebRequest 无法通过代理连接?

[英]HttpWebRequest cannot connect through proxy?

I am trying to achieve something that should be simple according to everything I have read, but is just not working for me: send any request through a proxy.根据我阅读的所有内容,我正在尝试实现一些应该很简单的东西,但对我不起作用:通过代理发送任何请求。

Please see the code below;请看下面的代码; it works as long as the 2 lines are commented out.只要注释掉 2 行,它就可以工作。 Once I put them back in and try to use any proxy the request constantly times out throwing an "Unable to connect to the remote server" WebException, inner exception message "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xxx.xxx.xxx:zzzz".一旦我把它们放回去并尝试使用任何代理,请求就会不断超时,抛出“无法连接到远程服务器”WebException,内部异常消息“连接尝试失败,因为连接方在一段时间后没有正确响应时间,或建立连接失败,因为连接的主机未能响应 xxx.xxx.xxx.xxx:zzzz”。

http://www.ip-adress.com/Proxy_Checker/ is used to get a list of test proxies. 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();

I am obviously missing something, and all similar questions I found either have more intricate issues or have been left unanswered.我显然遗漏了一些东西,我发现的所有类似问题要么有更复杂的问题,要么没有得到解答。

Thanks.谢谢。

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

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

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