如何从网络客户端获取状态代码? [英] How to get status code from webclient?

查看:33
本文介绍了如何从网络客户端获取状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebClient 类将一些数据发布到网络表单.我想获取表单提交的响应状态代码.到目前为止,我已经找到了在出现异常时如何获取状态代码

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 to get the status code if there is a exception

Catch wex As WebException
        If TypeOf wex.Response Is HttpWebResponse Then
          msgbox(DirectCast(wex.Response, HttpWebResponse).StatusCode)
            End If

但是如果表单提交成功并且没有抛出异常那么我就不会知道状态码(200,301,302,...)

However if the form is submitted successfully and no exception is thrown then I won't know the status code(200,301,302,...)

有没有什么方法可以在没有抛出异常的情况下获取状态码?

Is there some way to get the status code when there is no exceptions thrown?

PS:我不喜欢使用 httpwebrequest/httpwebresponse

PS: I prefer not to use httpwebrequest/httpwebresponse

推荐答案

试过了.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.

这篇关于如何从网络客户端获取状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆