HttpWebRequest: The request was aborted: The request was canceled

Emeka picture Emeka · Mar 17, 2010 · Viewed 68.1k times · Source

I've been working on developing a middle man application of sorts, which uploads text to a CMS backend using HTTP post requests for a series of dates (usually 7 at a time). I am using HttpWebRequest to accomplish this. It seems to work fine for the first date, but when it starts the second date I get the System.Net.WebException: The request was aborted: The request was canceled.

I've searched around and found the following big clues:

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/0d0afe40-c62a-4089-9d8b-fb4d206434dc

http://www.jaxidian.org/update/2007/05/05/8

http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html

And they haven't been too helpful. I've tried overloading the GetWebReuqest but that doesn't make sense because I don't make any use of that function.

Here is my code: http://pastebin.org/115268

I get the error on line 245 after it has run successfully at least once.

I'd appreciate any help I can get as this is the last step in a project I've been working on for sometime. This is my first C#/VS project so I'm open to any tips but I would like to focus on getting this problem solved first.

THanks!

Answer

Registered User picture Registered User · Jun 1, 2010

The common solution listed on the Internet appears to be to set the KeepAlive property of the HttpWebRequest to false. This can resolve the problem if the root cause is that the connection is expected to be reused even though it was actually closed automatically after some period of time. However, there is a performance hit to constantly opening and closing connections.

Another possible solution that I used when I encountered this problem was to extend the Timeout properties: WebRequest.ReadWriteTimeout, WebRequest.Timeout, RequestStream.WriteTimeout, and RequestStream.ReadTimeout. Please note these are in milliseconds, so you may want the timeouts to be 1000 * 60 * 10 to represent 10 minutes (or just 600000 if you think you will know what that means...). You can test if this is the more likely cause of the problem by reducing the file size.

BTW. Your code wasn't on the listed website anymore. You may want to include it in the text of your post if it is still an issue.