WebClient 是否使用 KeepAlive?

发布于 2024-10-12 07:14:54 字数 84 浏览 5 评论 0原文

我需要向单个主机发出大约 50 个 HTTP 请求(API 调用)。性能很重要,所以我想使用 HTTP KeepAlive。 WebClient 支持吗?

I need to issue around 50 HTTP requests to a single host (API calls). Performance is important, so I'd like to use HTTP KeepAlive's. Does WebClient support this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ˇ宁静的妩媚 2024-10-19 07:14:54

它在我的机器上运行,但我看不到它的记录。我当然希望它是默认的。最简单的判断方法是运行 Wireshark (或 Fiddler)并查看到底发生了什么。

例如,此程序:

using System;
using System.Net;

class Test
{    
    static void Main()
    {
        WebClient client = new WebClient();
        for (int i = 0; i < 50; i++)
        {
            string text = client.DownloadString("http://www.microsoft.com");
            Console.WriteLine(text.Length);
        }
    }
}

生成第一个请求:

GET / HTTP/1.1   
Host: www.microsoft.com    
Connection: Keep-Alive

子序列请求只是:

GET / HTTP/1.1
Host: www.microsoft.com

... 大概是因为一旦连接处于 KeepAlive 模式,就假定它将保持这种状态。

It does on my machine, but I can't see that it's documented to. I'd certainly expect it to by default. The simplest way to tell is to run Wireshark (or Fiddler) and look at exactly what's going down the wire.

For example, this program:

using System;
using System.Net;

class Test
{    
    static void Main()
    {
        WebClient client = new WebClient();
        for (int i = 0; i < 50; i++)
        {
            string text = client.DownloadString("http://www.microsoft.com");
            Console.WriteLine(text.Length);
        }
    }
}

Generates a first request of:

GET / HTTP/1.1   
Host: www.microsoft.com    
Connection: Keep-Alive

Subsequence requests are just:

GET / HTTP/1.1
Host: www.microsoft.com

... presumably because once a connection is in KeepAlive mode, it's assumed it will stay that way.

真心难拥有 2024-10-19 07:14:54

如此处所述,WebClient 在其私有实现中使用 WebRequest,http ://msdn.microsoft.com/en-us/library/system.net.webclient.aspx。 Microsoft 不会将其公开为公共财产供您控制。

因此,使用 Reflector 查看其实现,您可以看到如何为正在使用的 WebRequest 对象设置 KeepAlive。就像@Jon 指出的那样,实验表明 KeepAlive 设置为 true。这也符合其他场景,例如 .NET 远程处理的私有实现。

在极少数情况下,您可能会发现 KeepAlive = true 会导致 SocketException,然后您必须使用反射或其他技巧将其设置为 false,这非常烦人。

As documented here, WebClient makes use of WebRequest in its private implementation, http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx. Microsoft does not expose that as a public property for you to control.

Therefore, reviewing its implementation using Reflector you can see how KeepAlive is set for the WebRequest object in use. Like @Jon pointed out, an experiment shows that KeepAlive is set to true. This matches other scenarios too, such as .NET remoting's private implementation.

In rare cases you may find KeepAlive = true can lead to SocketException, and then you have to use reflection or other tricks to set it to false which is very annoying.

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