是否WebClient的使用的KeepAlive? [英] Does WebClient use KeepAlive?

查看:1580
本文介绍了是否WebClient的使用的KeepAlive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发出约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?

推荐答案

它做我的机器上,但我不能看到它的文件的到。我默认情况下它肯定期望。告诉最简单的方法是运行 Wireshark的(或小提琴手),看看到底发生了什么沿着电线。

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.

例如,这个方案:

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因为一旦连接保持活动状态,它的假设它会保持下去。

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

这篇关于是否WebClient的使用的KeepAlive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆