开发者

HttpWebRequest: cleaner access to numeric HTTP status codes?

开发者 https://www.devze.com 2023-01-28 00:46 出处:网络
When I try to get a nonexistent page, or use an invalid HTTP method, HttpWebRequest.GetResponse() throws a System.Net.WebException, w开发者_开发问答ith a Status property of ProtocolError. In its Messa

When I try to get a nonexistent page, or use an invalid HTTP method, HttpWebRequest.GetResponse() throws a System.Net.WebException, w开发者_开发问答ith a Status property of ProtocolError. In its Message property I can see the HTTP status code in parentheses. Fine. But I don't see an integer StatusCode property. Do I really have to parse the Message property to get it?


From the WebException.Status MSDN page:

((HttpWebResponse)your_exception.Response).StatusCode

The StatusCode property will give you an enum that maps to the regular status codes from the HTTP spec.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号