It seems that there are MANY ways to perform http web request poorly. This is a huge problem in today's world where web-services are more common than bankrupt banks. Here is a quick pattern of how to do it right:
public string Fetch(Uri requestUri){
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.Timeout = requestConnectTimeoutInMs; // take timeout from configwebRequest.ReadWriteTimeout = requestReadWriteTimeoutInMs; // take timeout from config
using (WebResponse webResponse = webRequest.GetResponse())using (StreamReader streamReader = new StreamReader(new TimeoutStream(webResponse.GetResponseStream(), fetchTimeoutInMs)) // take timeout from config return streamReader.ReadToEnd();
}
Details:
public override int Read(byte[] buffer, int offset, int count) { CheckTimeout(); // throw TimeoutException if timeout was reached return _stream.Read(buffer, offset, count); }
Multiple HttpWebRequest limitation:By default, you can't perform more than 2-3 async HttpWebRequest (depends on the OS). In order to override it (the easiest way, IMHO) don't forget to add this under <configuration> section in the application's config file:
<system.net> <connectionManagement> <add address="*" maxconnection="65000" /> </connectionManagement></system.net>Why should you follow these guidelines:
Important note about recycling HttpWebRequest.GetResposne()Simply put, it's not working by design. That means that if you fail to get a response on time (due to 1,2 or 3), don't call the webRequest.GetResponse() again as it is cached internally (you'll get the same HttpWebResposne). What you should do is to re-create the HttpWebRequest and try again. I don't agree with the selected design by Microsoft for this method, but at least it's good to be aware of it.
from MSDN:
" Multiple calls to GetResponse return the same response object; the request is not reissued. "
Final note:You should obviously consider writing a HttpWebRequestHelper class (or extension method) and use it instead of copy&paste this code all over your codebase.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2010, Oren Ellenbogen
<= Contact me via E-mail
newtelligence dasBlog 2.2.8279.16125