开发者

Abort request wp7

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(asynchronou开发者_Go百科sResult);//WebException if aborted
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            do_after(streamRead.ReadToEnd());
            streamResponse.Close();
            streamRead.Close();
            response.Close();
    //    }


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜