2014-10-02 94 views
-1

在我的.net應用程序中,我正在使用WebClient。我有響應回調類似於:.net WebClient http錯誤代碼

private void CBMethod(object sender, UploadStringCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
     //Process result 
    } 

}

現在,在錯誤的情況下,我要檢索的HTTP錯誤代碼。對於例如401未經授權的請求。我只想讓代碼

+0

你知道如何使用調試器..?設置通過代碼,當你得到如果聲明..檢查e。並查看其他屬性的暴露情況。 – MethodMan 2014-10-02 21:59:12

回答

0

你可以做這樣的事情來得到響應狀態代碼:

if (e.Error is System.Net.WebException) 
{ 
    var webException = (System.Net.WebException)e.Error; 
    var response = (System.Net.HttpWebResponse)webException.Response; 

    System.Net.HttpStatusCode status = response.StatusCode; 
}