Pergunta

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();
    //    }
Foi útil?

Solução

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)

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top