2010-06-03 32 views
0

httpRequest.GetResponse()方法獲取狀態碼422(不可處理的實體)並引發異常。在我使用的這個web服務中,我知道XML響應也被髮送,我需要得到這個響應才能找出服務器無法處理我的請求的原因。如何在收到HTTP狀態不是OK時收到XML響應?

如何在catch塊中獲取XML響應?

try 
{ 
    // Submits the HTTP request to create the invoice and gets the XML response. 
    using (HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse) 
    { 
     // my code... 
     return httpResponse; 
    } 
} 
catch (Exception e) 
{ 
} 

回答

2

捕獲WebException並訪問e.Response屬性。

+0

謝謝maciejkow!在這裏,另一個相關的問題在stackoverflow,也可能是有用的其他用戶試圖從異常得到響應http://stackoverflow.com/questions/1167913/webexception-when-reading-a-webexceptions-response-stream – 2010-06-03 13:16:32