[英]HttpWebResponse and Proxy w/ NetworkCredentials Not Working
I am working on a project that uses proxy to obtain websites' HTML code. 我正在一个使用代理获取网站HTML代码的项目。
Now, the trouble I have is that I want to use Username-Password authentication and not IP-Auth when connecting to the proxy. 现在,我遇到的麻烦是,我想在连接到代理时使用用户名密码验证而不是IP-Auth。
I have written a sample code and ran it with Snippy . 我已经编写了一个示例代码,并与Snippy一起运行了它。 It worked. 有效。 Then I copied the same code into a Visual Studio .NET 4.5 Project and it failed with the error: An existing connection was forcibly closed by the remote host
when trying to get the response. 然后,我将相同的代码复制到Visual Studio .NET 4.5项目中,但失败并显示以下错误:尝试获取响应时An existing connection was forcibly closed by the remote host
。
Here is the code: 这是代码:
WebProxy[] proxies = { new WebProxy("ip", port) };
proxies[0].Credentials = new NetworkCredential { UserName = "username", Password = "password" };
string url = "https://www.google.com/search?q=s&num=50";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = proxies[0];
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader response_stream = new StreamReader(res.GetResponseStream())
{
string html = response_stream.ReadToEnd();
Console.WriteLine(html);
}
}
I have tried different variations. 我尝试了不同的变化。 When I switch to authorization by IP, add mine and comment out the NetworkCredentials assignment, the code works perfectly both in Snippy and Visual Studio. 当我切换到IP授权时,添加我的并注释掉NetworkCredentials分配,该代码在Snippy和Visual Studio中都可以正常工作。
But why does it fail when using NetworkCredentials? 但是,为什么在使用NetworkCredentials时失败?
OK, I found the culprit. 好,我找到了罪魁祸首。 It was my proxy provider. 那是我的代理人。
Note for everybody ever running into problems when testing proxy connection with HttpWebRequest -- try another seller . 请注意在使用HttpWebRequest测试代理连接时遇到问题的每个人- 尝试其他卖家 。 It might save you a couple of minutes. 它可以节省您几分钟的时间。 Or hours. 或几个小时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.