[英]Abort request wp7
If I abort the request I have a WebException.如果我中止请求,我有一个 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();
// }
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. Abort
方法取消对资源的请求。 After a request is canceled, calling the BeginGetResponse
, EndGetResponse
, BeginGetRequestStream
, or EndGetRequestStream
method causes a WebException
with the Status
property set to RequestCanceled
.取消请求后,调用BeginGetResponse
、 EndGetResponse
、 BeginGetRequestStream
或EndGetRequestStream
方法会导致WebException
并将Status
属性设置为RequestCanceled
。
Source: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort(v=VS.95 )来源: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort(v=VS.95 )
You should wrap EndGetResponse in a try-catch block.您应该将 EndGetResponse 包装在 try-catch 块中。 If a web request is aborted you are facing an unexpected flow, therefore exceptions are the best way to handle this.如果 web 请求被中止,您将面临意外的流程,因此异常是处理此问题的最佳方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.