且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从网络客户端获取状态代码?

更新时间:2022-06-07 22:13:06

试过了.ResponseHeaders 不包括状态代码.

Tried it out. ResponseHeaders do not include status code.

如果我没记错的话,WebClient 能够在单个方法调用中抽象出多个不同的请求(例如,正确处理 100 个 Continue 响应、重定向等).我怀疑如果不使用 HttpWebRequestHttpWebResponse,可能无法使用不同的状态代码.

If I'm not mistaken, WebClient is capable of abstracting away multiple distinct requests in a single method call (e.g. correctly handling 100 Continue responses, redirects, and the like). I suspect that without using HttpWebRequest and HttpWebResponse, a distinct status code may not be available.

我突然想到,如果您对中间状态代码不感兴趣,您可以放心地假设最终状态代码在 2xx(成功)范围内,否则调用将不会成功.

It occurs to me that, if you are not interested in intermediate status codes, you can safely assume the final status code is in the 2xx (successful) range, otherwise, the call would not be successful.

不幸的是,ResponseHeaders 字典中没有状态代码.

The status code unfortunately isn't present in the ResponseHeaders dictionary.