21 January 2004

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(“http://www.microsoft.com“) 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.

Filed under:
 

Comments

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

for(int i=0; i < 3; i++) {
HttpWebRequest r = WebRequest.Create(“http://www.microsoft.com“) as HttpWebRequest;
using (HttpWebResponse w = r.GetResponse() as HttpWebResponse)
{
... do something with the response ...
}
}

22 January 04 at 12:37 AM
# Marshall Brooke said:
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("http://www.microsoft.com") 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]);
}
Console.WriteLine("*********************************************************************");
for(int j=0;j<w.Headers.Count;j++){
Console.WriteLine("{0} : {1}",w.Headers.GetKey(j),w.Headers[j]);
}
w.Close();
Thread.Sleep(TimeSpan.FromMinutes(2));
}

}
catch(Exception e) {
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
22 January 04 at 1:49 AM
# Feroze Daud said:
I am unable to reproduce this error. What build of the framework do you have ?
22 January 04 at 7:05 PM
# c#learner said:
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);
resStream.Close();

sText = Encoding.ASCII.GetString(buf, 0, buf.Length);
20 February 04 at 11:59 AM
# c#learner said:
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"
20 February 04 at 12:00 PM
# Feroze Daud said:
Hi!

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.


23 February 04 at 1:53 PM
# Andrew Davey said:
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()
reader.Close()
rs.Close()
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?
02 May 04 at 4:12 AM
# webreq.getresponse exceptions | keyongtech said:

PingBack from http://www.keyongtech.com/721854-webreq-getresponse-exceptions

21 January 09 at 7:57 PM
New Comments to this post are disabled
Page view tracker