How to get status code from webclient?

前端 未结 10 1881
轮回少年
轮回少年 2020-11-29 04:03

I am using the WebClient class to post some data to a web form. I would like to get the response status code of the form submission. So far I\'ve found out how

10条回答
  •  温柔的废话
    2020-11-29 04:45

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

    HttpStatusCode GetHttpStatusCode(System.Exception err)
    {
        if (err is WebException)
        {
            WebException we = (WebException)err;
            if (we.Response is HttpWebResponse)
            {
                HttpWebResponse response = (HttpWebResponse)we.Response;
                return response.StatusCode;
            }
        }
        return 0;
    }
    

提交回复
热议问题