2012-12-03 8 views
6

मैं नीचे इस विधि में अपवाद कैसे प्राप्त करूं?एसिंक्रोनस HttpWebRequest से अपवादों को पकड़ना एक कार्य में कॉल करता है

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, आदि त्रुटियों है:

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

मैं नहीं कर सकते यह पता लगाने के लिए उन्हें

उत्तर

11

को संभालने के लिए कैसे आपका त्रुटि शायद आपका प्रतिनिधि बुला request.EndGetResponse(asyncResult) में हो रहा है ।

आप उपयोग कर कार्य बना सकते हैं हालांकि:

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

जो काम करने के लिए किसी भी अपवाद का प्रचार करना चाहिए। वैकल्पिक रूप से

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

आप सी # 5 उपयोग कर रहे हैं तो आप उपयोग कर सकते हैं async/इंतजार अपने MakeAsyncRequest बनाने के लिए:

आप अपने ContinueWith प्रतिनिधि में त्रुटियों की जांच कर सकते हैं। यह आप के लिए AggregateException से अपवाद खोलने देगा:

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); 
} 
+0

अब यह वापसी task.ContinueWith में एक अपवाद देता है (टी => FinishWebRequest (t.Result)); "एक अयस्क अधिक त्रुटियां हुईं" – Jacqueline

+0

@ जैकक्लिन - आपका अनुरोध अपवाद फेंक रहा है - त्रुटियां क्या हैं? – Lee

+0

यह सिर्फ कहता है "एक या अधिक त्रुटियां हुईं।" – Jacqueline

0

तो अपने कार्य गलती राज्य के लिए अपने राज्य बदलता है और आप कई मायनों में इस त्रुटि की जाँच कर सकते हैं:

// Inside method MakeAsyncRequest 
Task<WebResponse> 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); 
    }; 

किसी भी मामले आप तैयार करना चाहिए में किसी भी कार्य असफल हो सकता है कि, तो आप के रूप में अच्छी कार्यों जिसके परिणामस्वरूप को संभालने के लिए उसी तकनीक का उपयोग करना चाहिए:

// 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); 
} 
संबंधित मुद्दे