System.Net.WebException: The request was aborted: The request was canceled
In one of our programs I had to fetch some data in a loop from a SAP WebService in a .NET 3.5 application. However, for some requests the “The request was aborted: The request was canceled” WebException was thrown.
It took me a while to find the solution and there are few unsolved threads in google’s top results, so I’ll post the solution that worked for me. In my case, it could be solved by setting KeepAlive setting of the HttpWebRequest to false.
Based on the article explaining how to do it in Visual Basic for .NET (and why) and this thread, if you are using WebService, you can do it this way:
- In Visual Studio Solution Explorer toggle on Show all files button
- Navigate to the WebService reference which causes problems, find and open Reverence.cs file (or Reference.vb for Visual Basic)
- Add an overriden method to the file:
1 2 3 4 5 6 |
protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri); webRequest.KeepAlive = false; return webRequest; } |
or in Visual Basic:
1 2 3 4 5 6 7 8 9 10 |
Protected Overrides Function GetWebRequest(ByVal uri As Uri) _ As WebRequest Dim webRequest As HttpWebRequest = CType( _ MyBase.GetWebRequest(uri), _ HttpWebRequest) webRequest.KeepAlive = False Return webRequest End Function |
You only need to remember, that the file will be re-generated when you update the web reference, so in that case you will have to apply the trick again.
que fuerte!
hi, i am following below url for sap services:
http://www.c-sharpcorner.com/uploadfile/chinnasrihari/consming-sap-web-services-and-net/
after implementation , getting the “the request was aborted the request was canceled”. please help me out to come out of this issue.
hi, I guess that my only knowledge on how to solve this problem is the one you can read in the article above 😉 good luck!