[英]Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server?
Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server? 如果服务器返回200以外的其他值,HttpWebRequest.GetResponse()是否总是抛出WebException吗?
I saw this question but the answers were not conclusive. 我看到了这个问题,但答案并不完全确定。
No, WebException will only be thrown in certain conditons, mainly because of timeouts and errors while processing the request. 不,WebException只会在某些情况下抛出,主要是因为在处理请求时超时和错误。
Here is the documentation for HttpWebRequest.GetResponse(): 这是HttpWebRequest.GetResponse()的文档:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx
Note that there are three main reasons why a WebException will be thrown: 请注意,引发WebException的三个主要原因:
Abort was previously called.
-or-
The time-out period for the request expired.
-or-
An error occurred while processing the request.
The bottom two are the most common, and you'll see 400's from these most often. 倒数第二个是最常见的,您会经常从中看到400个。 In the documentation, they have a good recommendation in which you can use to try to diagnose the exact cause of the issue:
在文档中,他们有很好的建议,您可以用来尝试诊断问题的确切原因:
If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.
如果引发WebException,请使用异常的Response和Status属性来确定服务器的响应。
If you're using an IDE (visual studio) where you can place a breakpoint, examine the response and status. 如果您使用的是可以放置断点的IDE(Visual Studio),请检查响应和状态。 If not, use Console.Writeline() to print out the response or status code and start investigating from there.
如果不是,请使用Console.Writeline()打印响应或状态代码,然后从那里开始调查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.