.net WebClient http错误代码

本文关键字:错误代码 http WebClient net | 更新日期: 2023-09-27 18:25:27

在我的.net应用程序中,我正在使用WebClient。我有类似于的回复电话

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

}

现在,如果出现错误,我想检索http错误代码。例如401未经授权的请求。我只想得到代码

.net WebClient http错误代码

您可以执行以下操作从响应中获取状态代码:

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;
}