2

If I abort the request I have a WebException. How I can check if request is aborted?

// if (asynchronousResult.AsyncState !="Aborted" ) {
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);//WebException if aborted
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            do_after(streamRead.ReadToEnd());
            streamResponse.Close();
            streamRead.Close();
            response.Close();
    //    }
SevenDays
  • 3,718
  • 10
  • 44
  • 71

2 Answers2

4
try
{
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);//WebException if aborted
}
catch(WebException e)
{
  if(e.Status == WebExceptionStatus.RequestCanceled)
    {
      //WORK
    }
}

From Documentation:

The Abort method cancels a request to a resource. After a request is canceled, calling the BeginGetResponse, EndGetResponse, BeginGetRequestStream, or EndGetRequestStream method causes a WebException with the Status property set to RequestCanceled.

Source: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort(v=VS.95)

Skomski
  • 4,827
  • 21
  • 30
1

You should wrap EndGetResponse in a try-catch block. If a web request is aborted you are facing an unexpected flow, therefore exceptions are the best way to handle this.

alf
  • 18,372
  • 10
  • 61
  • 92