System.Net.WebException HTTP status code

前端 未结 6 1900
执笔经年
执笔经年 2020-12-02 11:27

Is there an easy way to get the HTTP status code from a System.Net.WebException?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 12:24

    You can try this code to get HTTP status code from WebException. It works in Silverlight too because SL does not have WebExceptionStatus.ProtocolError defined.

    HttpStatusCode GetHttpStatusCode(WebException we)
    {
        if (we.Response is HttpWebResponse)
        {
            HttpWebResponse response = (HttpWebResponse)we.Response;
            return response.StatusCode;
        }
        return null;
    }
    

提交回复
热议问题