Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpWebRequest times out on second call
    text
    copied!<p>Why does the following code Timeout the second (and subsequent) time it is run?</p> <p>The code hangs at:</p> <pre><code>using (Stream objStream = request.GetResponse().GetResponseStream()) </code></pre> <p>and then causes a WebException saying that the request has timed out.</p> <p>I have tried this with a <code>WebRequest</code> and <code>HttpWebRequest</code></p> <p>Edit: It seems the code is falling over in <code>request.GetResponse()</code></p> <p>Edit: This post suggests it may be a GC issue --> <a href="http://www.vbforums.com/showthread.php?t=610043" rel="noreferrer">http://www.vbforums.com/showthread.php?t=610043</a> - as per this post the issue is mitigated if Fiddler is open in the background.</p> <p>The server is there and available for requests.</p> <pre><code> private string GetQLMResponse(string URL) { HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest; request.Credentials = new NetworkCredential(Settings.Default.LicenseUser, Settings.Default.LicensePassword); request.KeepAlive = false; request.Timeout = 5000; request.Proxy = null; // Read stream string responseString = String.Empty; try { using (var response = request.GetResponse()) { using (Stream objStream = response.GetResponseStream()) { using (StreamReader objReader = new StreamReader(objStream)) { responseString = objReader.ReadToEnd(); objReader.Close(); } objStream.Flush(); objStream.Close(); } response.Close(); } } catch (WebException ex) { throw new LicenseServerUnavailableException(); } finally { request.Abort(); request = null; GC.Collect(); } return responseString; } </code></pre> <p>Thrown WebException is:</p> <blockquote> <p>{"The operation has timed out"} [System.Net.WebException]: {"The operation has timed out"} Data: {System.Collections.ListDictionaryInternal} HelpLink: null InnerException: null Message: "The operation has timed out" Source: "System" StackTrace: " at System.Net.HttpWebRequest.GetResponse()\r\n at IQX.Licensing.License.GetQLMResponse(String URL) in C:\Users\jd\SVN\jd\Products\Development\JAD.Licensing\JAD.Licensing\License.cs:line 373" TargetSite: {System.Net.WebResponse GetResponse()}</p> </blockquote> <hr> <p>Update: OK So the following code now works. The servicePoint was setting the timeout to be near 4 minutes. Changing <code>ServicePoint.ConnectionLeaseTimeout</code> on the request object means that the request is now destroyed after 5000ms. Thanks to all for your help and also to these 2 pages:</p> <ol> <li><a href="http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx" rel="noreferrer">http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx</a></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/6hszazfz(v=VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/6hszazfz(v=VS.80).aspx</a></p> <pre><code>private string GetQLMResponse(string URL) { HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest; request.Credentials = new NetworkCredential(Settings.Default.LicenseUser, Settings.Default.LicensePassword); request.KeepAlive = false; request.Timeout = 5000; request.Proxy = null; request.ServicePoint.ConnectionLeaseTimeout = 5000; request.ServicePoint.MaxIdleTime = 5000; // Read stream string responseString = String.Empty; try { using (WebResponse response = request.GetResponse()) { using (Stream objStream = response.GetResponseStream()) { using (StreamReader objReader = new StreamReader(objStream)) { responseString = objReader.ReadToEnd(); objReader.Close(); } objStream.Flush(); objStream.Close(); } response.Close(); } } catch (WebException ex) { throw new LicenseServerUnavailableException(); } finally { request.Abort(); } return responseString; } </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload