我正在工作的一个项目,使用代理获得网站的HTML代码。
现在,我遇到的问题是,在连接到代理时,我希望使用用户名-密码身份验证,而不是IP。
我编写了一个示例代码,并使用急躁运行它。啊,真灵。然后,我将相同的代码复制到Visual .NET 4.5项目中,当试图获得响应时,它发生了错误:An existing connection was forcibly closed by the remote host失败。
以下是代码:
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);
}
}我试过不同的变体。当我切换到IP授权,添加我的授权并注释掉NetworkCredentials分配时,代码在Studio中都能很好地工作。
但是为什么它在使用NetworkCredentials时会失败呢?
发布于 2013-11-20 17:47:44
好吧,我找到凶手了。是我的代理供应商。
注意,当使用HttpWebRequest测试代理连接时,每个人都会遇到问题可能会节省你几分钟的时间。或者几个小时。
https://stackoverflow.com/questions/20091642
复制相似问题