Csharp/C#教程:从任务中的异步HttpWebRequest调用捕获exception分享


从任务中的异步HttpWebRequest调用捕获exception

我如何在下面的方法中捕获exception?

private static Task MakeAsyncRequest(string url) { if (!url.Contains("http")) url = "https://" + url; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Method = "GET"; request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; Task task = Task.Factory.FromAsync( request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), (object)null); return task.ContinueWith(t => FinishWebRequest(t.Result)); } 

我得到404,403等错误的具体地点是:

 Task task = Task.Factory.FromAsync( request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), (object)null); 

我无法弄清楚如何处理它们

您的错误可能发生在您的委托调用request.EndGetResponse(asyncResult)

但是您可以使用以下命令创建任务:

 Task task = Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null); 

这应该传播任务的任何例外。

您可以在ContinueWith委托中检查错误:

 return task.ContinueWith(t => { if (t.IsFaulted) { //handle error Exception firstException = t.Exception.InnerExceptions.First(); } else { return FinishWebRequest(t.Result); } }); 

或者,如果您使用的是C#5,则可以使用async / await创建MakeAsyncRequest 。 这将为您从AggregateException解除exception:

 private static async Task MakeAsyncRequest(string url) { if (!url.Contains("http")) url = "https://" + url; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Method = "GET"; request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; Task task = Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null); WebResponse response = await task; return FinishWebRequest(response); } 

因此,您的任务将其状态更改为Faulted状态,您可以通过以下几种方式检查此错误:

 // Inside method MakeAsyncRequest Task task = Task.Factory.FromAsync( request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), (object)null); // this 'task' object may fail and you should check it return task.ContinueWith( t => { if (t.Exception != null) FinishWebRequest(t.Result)) // Not the best way to fault "continuation" task // but you can wrap this into your special exception // and add original exception as a inner exception throw t.Exception.InnerException; // throw CustomException("The request failed!", t.Exception.InnerException); }; 

在任何情况下,您都应该准备任何任务可能失败,因此您应该使用相同的技术来处理结果任务:

上述就是C#学习教程:从任务中的异步HttpWebRequest调用捕获exception分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 // outside method MakeAsyncRequest var task = MakeAsyncRequest(string url); task.ContinueWith(t => // check tasks state or use TaskContinuationOption // handing error condition and result ); try { task.Wait(); // will throw Console.WriteLine(task.Result); // will throw as well } catch(AggregateException ae) { // Note you should catch AggregateException instead of // original excpetion Console.WriteLine(ae.InnerException); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/962599.html

(0)
上一篇 2021年11月27日
下一篇 2021年11月27日

精彩推荐