哪些特定的状态代码导致HttpWebRequest.GetResponse()引发WebException? [英] Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()?
问题描述
我已经寻找了一些明确的文档,但是运气不好.
I've hunted around for some definitive documentation on this but haven't had much luck finding any.
在执行类似POST的操作后,HttpWebRequest.GetResponse()
将针对哪些HTTP响应状态代码生成WebException
?
For which HTTP Response Status codes will HttpWebRequest.GetResponse()
generate a WebException
after doing something like a POST?
具体地说,它将为状态200 OK
以外的其他内容生成WebException
吗?还是只会生成WebException
来表示400、404和500(出于争论的目的)?
Specifically, will it generate a WebException
for anything other than status 200 OK
? Or will it only generate a WebException
for say, 400, 404, and 500 (for the sake of argument)?
我想知道,因为我正在与之通信的服务器定义了除HTTP 200 OK
以外的其他任何内容,它们会作为错误条件返回.我可以依靠WebException
生成200以外的任何东西吗?
I want to know since the server I'm communicating with defines anything other than HTTP 200 OK
coming back as an error condition. Can I rely on a WebException
being generated for anything other than 200?
我目前已经编写了代码,每次都要检查返回状态代码,并确保它为200 OK
.如果不是这样,将采取适当的措施—但是对于我希望清除的WebException
,该代码与catch块之间有很多重复.
I've currently written my code to check the return status code every time and ensure it's 200 OK
. If it's not, it will take appropriate action—but there's a lot of duplication between that code and the catch block for a WebException
that I'm hoping to clean up.
与文档相关的任何链接将不胜感激.
Any relevant links to documentation would be most appreciated.
推荐答案
最终在响应&捕获并检查WebExceptions;导致某些代码重复,但是对于状态不是200的WebException是否总是发生,没有明确的答案.
Ended up doing an explicit check after the response & catching and checking WebExceptions; results in some duplicated code but there's no definitive answer on whether a WebException will ALWAYS occur if the status is NOT 200.
这篇关于哪些特定的状态代码导致HttpWebRequest.GetResponse()引发WebException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!