HttpWebRequest and Connections

The .NET Framework has a HttpWebRequest object in the System.Net namespace. This object allows you to talk to HTTP Servers.

Usually, people forget that the underlying connection created by the request is not freed up,unless you call Close() on the response. So, you hit the connection limit, and no more webrequests will go through. This is illustrated in the following example:

for(int i=0; i < 3; i++) {
HttpWebRequest r = WebRequest.Create(““) as HttpWebRequest;
HttpWebResponse w = r.GetResponse() as HttpWebResponse;

The above code is buggy, because the third request is going to hang in GetResponse().

If you close the response, by calling Response.Close() , then the underlying connection gets freed up, and the request will succeed.

Comments (8)

  1. Me says:

    Since HttpWebResponse implements IDisposable, the using syntax would be appropriate:

    for(int i=0; i < 3; i++) {

    HttpWebRequest r = WebRequest.Create(““) as HttpWebRequest;

    using (HttpWebResponse w = r.GetResponse() as HttpWebResponse)


    … do something with the response …



  2. Marshall Brooke says:

    If the server in question is an HTTP 1.1 server and you close the response every time in the loop plus add a delay of say 5 minutes at the end of the loop (Thread.Sleep), I get a nasty error when the server closes the connection (after it’s Keep-Alive timeout). Does anyone have the same problem. The following code produces the error.<br/>

    try {

    for(int i=0; i < 3; i++) {

    HttpWebRequest r = WebRequest.Create(";) as HttpWebRequest;

    HttpWebResponse w = r.GetResponse() as HttpWebResponse;

    for(int j=0;j<r.Headers.Count;j++){

    Console.WriteLine("{0} : {1}",r.Headers.GetKey(j),r.Headers[j]);



    for(int j=0;j<w.Headers.Count;j++){

    Console.WriteLine("{0} : {1}",w.Headers.GetKey(j),w.Headers[j]);






    catch(Exception e) {





  3. Feroze Daud says:

    I am unable to reproduce this error. What build of the framework do you have ?

  4. c#learner says:

    Hi, This is my code, and it hangs randomly

    byte[] buf = new byte[8192];

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    Stream resStream = response.GetResponseStream();

    int count = resStream.Read(buf, 0, buf.Length);


    sText = Encoding.ASCII.GetString(buf, 0, buf.Length);

  5. c#learner says:

    Any ideas why the httpWebrequest.GetReponse() hangs? This error is not consistent, sometimes it works for a thousand requests and then fails, it gives the error "The operation timed out"

  6. Feroze Daud says:


    There are many reasons why GetResponse() might hang. Some are:

    1) You have reached the connection limit on the client ( 2 conns per http/1.1 server, 4 connections per http/1.0 server, or custom limit set on the ServicePoint), and no connection is free to send a request to the server. In other words, you might have 2 requests outstanding (eg: to a 1.1 server) which are taking more than 2 minutes to complete, and then issue another GetResponse() to the same server. The last getresponse() might time out.

    2) You have not closed the response stream of an earlier httpwebresponse. So, even if a connection is free, the request wont be sent.

    3) The server is taking too long to send the response.

    The best way to debug this is to use a network sniffer (for eg: NetworkMonitor) which ships with Windows NT (if you are running on NT platforms). Or you can use free sniffers which are available on the web.

  7. Andrew Davey says:

    I am trying to a make an app that spawns a set of worker threads (in the threadpool) that then go off and use an HttpWebRequest. This is the worker thread code:

    Private Sub DoWork(ByVal state As Object)

    If Not OnWorkerStart Is Nothing Then

    OnWorkerStart.Invoke(Me, Thread.CurrentThread)

    End If

    If Not _isCancelled Then

    Dim uri As String = String.Format(_data)

    Dim rq As HttpWebRequest = WebRequest.Create(uri)

    Dim rs As HttpWebResponse = rq.GetResponse()

    Dim stream As Stream = rs.GetResponseStream()

    Dim reader As New StreamReader(stream)

    _result = reader.ReadToEnd()



    End If

    If Not OnWorkerEnd Is Nothing Then

    OnWorkerEnd.Invoke(Me, Thread.CurrentThread)

    End If

    End Sub

    Is this the best practice? Or would it be better to stick with a single synchronous method where each request is called in turn?