Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

c# - Does WebClient use KeepAlive?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...