如何从Web客户端的状态code?客户端、状态、Web、code

2023-09-02 01:40:23 作者:对你的以后不打扰i

我现在用的是 Web客户端类张贴一些数据到Web表单。我想获得表单提交的响应状态code。到目前为止,我已经找到了如何让状态code,如果有一个例外

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

但是,如果表单提交成功,并没有异常被抛出,然后我就不知道了状态code(200301302 ...)

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

有没有办法让状态code时,有没有异常抛出?

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

PS:我preFER不使用的HttpWebRequest / httpwebresponse

PS: I prefer not to use httpwebrequest/httpwebresponse

推荐答案

尝试过了。 responseHeaders响应不包括状态code。

Tried it out. ResponseHeaders do not include status code.

如果我没有记错, Web客户端能抽象出多个不同的请求,在一个单一的方法调用(如正确处理100继续响应,重定向等)。我怀疑,如果没有使用的HttpWebRequest HttpWebResponse ,明显的状态code可能无法使用。

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.

它发生,我认为,如果你不感兴趣的中间状态codeS,你可以放心地假设最终状态code是在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.

状态code不幸的是,不是present在 responseHeaders响应字典。

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