Pages

Tuesday, July 26, 2011

How to resolve - "HttpWebRbRequest: The request was aborted: The request was cancelled." error.

Yesterday, I came across this new error message when doing HttpWebRbRequests in C# (.NET 3.5).

After doing a quick search, I see that it is a common problem with several solutions that has worked for different applications. This link here points to different solutions: HttpWebRequest: The request was aborted: The request was canceled

In my application, I have upto 16 identical threads doing simultaneous HTTP requests. However, each of these threads are requesting from different webservers along with unique local endpoints. Now the function making these calls has 3 successive HTTP requests to make (to the same webserver).

None of the above solutions worked for me, but the following combination did however.


System.Net.ServicePointManager.DefaultConnectionLimit = 200;
System.Net.ServicePointManager.MaxServicePointIdleTime = 2000;
System.Net.ServicePointManager.MaxServicePoints = 1000;
System.Net.ServicePointManager.SetTcpKeepAlive(false, 0, 0);

HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create("http://" + gatewayIP
 + "/xslt?PAGE=J04");
webRequest1.KeepAlive = false;
webRequest1.Timeout = 2000;
//Do other stuff here...
//Close response stream

Thread.Sleep(2000); //This delay seems to help.

HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("http://" + gatewayIP
 + "/xslt?PAGE=J04");
webRequest2.KeepAlive = false;
webRequest2.Timeout = 2000;
//Do other stuff here...
//and so on...




1 comment: