WebRequest.GetResponse 需要很长时间,除非 DefaultWebProxy 为空

发布于 2024-12-04 07:39:17 字数 1067 浏览 1 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-11 07:39:17

我已经看到了这个,需要一些发现才能解决。

就我而言,发生的情况是有人在“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文