从异步HttpWebRequest的捕获异常的任务调用 [英] Catching exceptions from Asynchronous HttpWebRequest Calls in a Task

查看:522
本文介绍了从异步HttpWebRequest的捕获异常的任务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会赶在下面这种方法的例外?

How would i catch the exception in this method below?

    private static Task<string> MakeAsyncRequest(string url)
    {
        if (!url.Contains("http"))
            url = "http://" + 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<WebResponse> task = Task.Factory.FromAsync(
        request.BeginGetResponse,
        asyncResult => request.EndGetResponse(asyncResult),
        (object)null);

        return task.ContinueWith(t => FinishWebRequest(t.Result));

    }



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

The specific place i am getting 404, 403, etc errors is:

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



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

I cant figure out how to handle them

推荐答案

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

Your error is probably happening in your delegate calling request.EndGetResponse(asyncResult).

但是,您可以创建一个使用任务:

However you can create the task using:

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



这应该传播任何异常的任务。

which should propagate any exceptions to the task.

您可以检查你的 ContinueWith 委托错误:

You can check for errors in your ContinueWith delegate:

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



另外,如果你使用C#5,那么你可以使用异步/的await创建 MakeAsyncRequest 。这将解开从异常的 AggregateException 为您提供:

Alternatively if you're using C#5 then you can use async/await to create your MakeAsyncRequest. This will unwrap the exception from the AggregateException for you:

private static async Task<string> MakeAsyncRequest(string url)
{
    if (!url.Contains("http"))
        url = "http://" + 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<WebResponse> task = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
    WebResponse response = await task;
    return FinishWebRequest(response);
}

这篇关于从异步HttpWebRequest的捕获异常的任务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆