C#在异常中获取HttpStatusCode用法

来源:互联网 发布:什么叫分布式系统 知乎 编辑:程序博客网 时间:2024/06/10 14:21

HttpStatusCode用法
原来我的写法是:

            catch (Exception ex)            {                msg = ex.ToString();                return false;            }

发现这么写没法获取到status code,所以参考该文章,取WebException e,即可。

catch (WebException e){       string status = null;       HttpWebResponse response = (HttpWebResponse)e.Response;       if (response.StatusCode == HttpStatusCode.NotFound)             status = "404";       if (response.StatusCode == HttpStatusCode.InternalServerError)             status = "500";       if (response.StatusCode == HttpStatusCode.BadGateway)             status = "502";}
原创粉丝点击