WebRequest.GetResponse 需要很长时间,除非 DefaultWebProxy 为空
我使用的是 Windows 7 Ultimate x64。
我首先进行了浏览,发现 this 在那里我得到了“解决方案” ' 的问题,但想知道为什么 - 我不想将 DefaultWebProxy 设为 null,以防存在应该使用的合法代理。 (顺便说一句,我不在http代理后面)
这是一个简单的单元测试,演示了这个问题。
[Test]
public void TestWebRequest()
{
//if I dont include the following line the request takes ~40 seconds.
WebRequest.DefaultWebProxy = null;
var httpRequest = WebRequest.Create("http://google.com");
var stopWatch = new Stopwatch();
stopWatch.Start();
using (var webResponse = httpRequest.GetResponse())//this is the line taking ages.
{
using (var sr = new StreamReader(webResponse.GetResponseStream()))
Trace.WriteLine(sr.ReadToEnd());
}
stopWatch.Stop();
Trace.WriteLine(string.Format("took {0} sec", stopWatch.Elapsed.TotalSeconds));
}
我们能不能不要陷入不可处理的原因之中?
I'm using Windows 7 Ultimate x64.
I've first had a browse and found this where I got the 'solution' to the issue but would like to know why - I dont want to null the DefaultWebProxy in case there is a legitimate proxy that should be used. (btw I am not behind an http proxy)
Here is a simple unit test that demostrates the issue.
[Test]
public void TestWebRequest()
{
//if I dont include the following line the request takes ~40 seconds.
WebRequest.DefaultWebProxy = null;
var httpRequest = WebRequest.Create("http://google.com");
var stopWatch = new Stopwatch();
stopWatch.Start();
using (var webResponse = httpRequest.GetResponse())//this is the line taking ages.
{
using (var sr = new StreamReader(webResponse.GetResponseStream()))
Trace.WriteLine(sr.ReadToEnd());
}
stopWatch.Stop();
Trace.WriteLine(string.Format("took {0} sec", stopWatch.Elapsed.TotalSeconds));
}
Can we please not get bogged down on IDisposable causes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经看到了这个,需要一些发现才能解决。
就我而言,发生的情况是有人在“Internet 连接选项”的代理字段中输入了“”(空格)并打开使用代理。
这很奇怪,因为它只会导致代理第一次启动时出现长时间延迟,并且如果您返回互联网设置,它不会显示其中存在或曾经有空格。
I have seen this and it took some finding to solve.
In my case what happened was someone had entered a " " (space) into the proxy field in the Internet Connection Options and turned on to use Proxy.
It's bizarre, because it only causes the long delay on the 1st start of the Proxy, and if you go back to the internet settings it doesn't show that there is or was a space in there.