Welcome to MSDN Blogs Sign in | Join | Help

Feroze Daud's WebLog

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.

Published Wednesday, January 21, 2004 8:32 PM by Feroze Daud

Filed under:

Comments

# re: HttpWebRequest and Connections @ Thursday, January 22, 2004 12:37 AM

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 ...
}
}

Me

# re: HttpWebRequest and Connections @ Thursday, January 22, 2004 1:49 AM

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();
}

Marshall Brooke

# re: HttpWebRequest and Connections @ Thursday, January 22, 2004 7:05 PM

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

Feroze Daud

# re: HttpWebRequest and Connections @ Friday, February 20, 2004 11:59 AM

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);

c#learner

# re: HttpWebRequest and Connections @ Friday, February 20, 2004 12:00 PM

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"

c#learner

# re: HttpWebRequest and Connections @ Monday, February 23, 2004 1:53 PM

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.


Feroze Daud

# re: HttpWebRequest and Connections @ Sunday, May 02, 2004 4:12 AM

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?

Andrew Davey

# webreq.getresponse exceptions | keyongtech @ Wednesday, January 21, 2009 7:57 PM

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

webreq.getresponse exceptions | keyongtech

New Comments to this post are disabled
 
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker