HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspxThe .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 responseen-USTelligent Evolution Platform Developer Build (Build: 5.6.50428.7875)webreq.getresponse exceptions | keyongtechhttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#9360836Thu, 22 Jan 2009 03:57:36 GMT91d46819-8472-40ad-a661-2c78acb4018c:9360836webreq.getresponse exceptions | keyongtech<p>PingBack from <a rel="nofollow" target="_new" href="http://www.keyongtech.com/721854-webreq-getresponse-exceptions">http://www.keyongtech.com/721854-webreq-getresponse-exceptions</a></p>
<img src="http://blogs.msdn.com/aggbug.aspx?PostID=9360836" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#124688Sun, 02 May 2004 11:12:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:124688Andrew DaveyI 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:
<br>
<br> Private Sub DoWork(ByVal state As Object)
<br> If Not OnWorkerStart Is Nothing Then
<br> OnWorkerStart.Invoke(Me, Thread.CurrentThread)
<br> End If
<br>
<br> If Not _isCancelled Then
<br> Dim uri As String = String.Format(_data)
<br>
<br> Dim rq As HttpWebRequest = WebRequest.Create(uri)
<br> Dim rs As HttpWebResponse = rq.GetResponse()
<br> Dim stream As Stream = rs.GetResponseStream()
<br> Dim reader As New StreamReader(stream)
<br> _result = reader.ReadToEnd()
<br> reader.Close()
<br> rs.Close()
<br> End If
<br>
<br> If Not OnWorkerEnd Is Nothing Then
<br> OnWorkerEnd.Invoke(Me, Thread.CurrentThread)
<br> End If
<br> End Sub
<br>
<br>Is this the best practice? Or would it be better to stick with a single synchronous method where each request is called in turn?<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=124688" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#78697Mon, 23 Feb 2004 21:53:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:78697Feroze DaudHi!
<br>
<br>There are many reasons why GetResponse() might hang. Some are:
<br>
<br>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.
<br>
<br>2) You have not closed the response stream of an earlier httpwebresponse. So, even if a connection is free, the request wont be sent.
<br>
<br>3) The server is taking too long to send the response.
<br>
<br>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.
<br>
<br>
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=78697" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#77246Fri, 20 Feb 2004 20:00:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:77246c#learnerAny 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 &quot;The operation timed out&quot;<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=77246" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#77245Fri, 20 Feb 2004 19:59:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:77245c#learnerHi, This is my code, and it hangs randomly
<br>
<br>byte[] buf = new byte[8192];
<br>HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
<br>HttpWebResponse response = (HttpWebResponse)request.GetResponse();
<br>
<br>Stream resStream = response.GetResponseStream();
<br>
<br>int count = resStream.Read(buf, 0, buf.Length);
<br>resStream.Close();
<br>
<br>sText = Encoding.ASCII.GetString(buf, 0, buf.Length);<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=77245" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#61970Fri, 23 Jan 2004 03:05:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:61970Feroze DaudI am unable to reproduce this error. What build of the framework do you have ?
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=61970" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#61538Thu, 22 Jan 2004 09:49:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:61538Marshall BrookeIf 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.&lt;br/&gt;
<br>try {
<br> for(int i=0; i &lt; 3; i++) {
<br> HttpWebRequest r = WebRequest.Create(&quot;<a target="_new" href="http://www.microsoft.com&quot;">http://www.microsoft.com&quot;</a>) as HttpWebRequest;
<br> HttpWebResponse w = r.GetResponse() as HttpWebResponse;
<br> for(int j=0;j&lt;r.Headers.Count;j++){
<br> Console.WriteLine(&quot;{0} : {1}&quot;,r.Headers.GetKey(j),r.Headers[j]);
<br> }
<br> Console.WriteLine(&quot;*********************************************************************&quot;);
<br> for(int j=0;j&lt;w.Headers.Count;j++){
<br> Console.WriteLine(&quot;{0} : {1}&quot;,w.Headers.GetKey(j),w.Headers[j]);
<br> }
<br> w.Close();
<br> Thread.Sleep(TimeSpan.FromMinutes(2));
<br> }
<br>
<br> }
<br> catch(Exception e) {
<br> Console.WriteLine(e.Message);
<br> }
<br> Console.ReadLine();
<br> }<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=61538" width="1" height="1">re: HttpWebRequest and Connectionshttp://blogs.msdn.com/b/feroze_daud/archive/2004/01/21/61400.aspx#61516Thu, 22 Jan 2004 08:37:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:61516MeSince HttpWebResponse implements IDisposable, the using syntax would be appropriate:
<br>
<br>for(int i=0; i &lt; 3; i++) {
<br>HttpWebRequest r = WebRequest.Create(“<a target="_new" href="http://www.microsoft.com">http://www.microsoft.com</a>“) as HttpWebRequest;
<br>using (HttpWebResponse w = r.GetResponse() as HttpWebResponse)
<br>{
<br> ... do something with the response ...
<br>}
<br>}
<br>
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=61516" width="1" height="1">