Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WebClient use KeepAlive?

Tags:

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?

like image 450
Vilx- Avatar asked Jan 15 '11 10:01

Vilx-


People also ask

How do I know if Keep-Alive is working?

All modern browsers use persistent connections as long as the server has Keep-Alive enabled. In order to check if your pages are delivered with a Keep-Alive header, you can use the HTTP Header Checker tool. This will display the Connection: Keep-Alive field if the HTTP Keep-Alive header is enabled.

How do I enable Keep-Alive?

To enable Keep-Alive, you need to explicitly request it via the HTTP header by accessing . htaccess or the main configuration file of your web server. If you turn on Keep-Alive, the HTTP response header will show Connection: keep-alive.

How does HTTP keep alive work?

HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.

What is http keep alive timeout?

The keep alive timeout on the Message Processor allows a single TCP connection to send and receive multiple HTTP requests/responses from/to the backend server, instead of opening a new connection for every request/response pair.


2 Answers

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.

like image 57
Jon Skeet Avatar answered Sep 23 '22 05:09

Jon Skeet


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.

like image 26
Lex Li Avatar answered Sep 21 '22 05:09

Lex Li