简体   繁体   English

如果服务器返回200以外的其他值,HttpWebRequest.GetResponse()是否总是抛出WebException吗?

[英]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.

相关问题 HttpWebRequest.GetResponse(),是否除了200之外的每个StatusCode都会引发异常? - HttpWebRequest.GetResponse() , does every StatusCode besides 200 throws exception? 当HttpWebRequest.GetResponse抛出WebException时,如何读取返回的自定义错误消息? - How do I read a custom error message returned when HttpWebRequest.GetResponse throws a WebException? 哪些特定的状态代码会导致HttpWebRequest.GetResponse()抛出WebException? - Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()? HttpWebRequest.GetResponse提供内部服务器错误 - HttpWebRequest.GetResponse giving Internal Server Error 获取错误“使用HttpWebRequest.GetResponse()进行屏幕抓取时,远程服务器返回错误:(403)禁止” - Getting Error “The remote server returned an error: (403) Forbidden” when screen scraping using HttpWebRequest.GetResponse() C#HttpWebRequest.GetResponse()返回错误:(404)找不到 - c# HttpWebRequest.GetResponse() returned an error: (404) Not Found HttpWebRequest.GetResponse 无论如何总是超时 - HttpWebRequest.GetResponse always times out no matter what System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() C#HttpWebRequest.GetResponse - 如何处理非异常与webexception响应的StatusCode用法? - C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response? 我正在获取WebException:“操作已超时”立即在HttpWebRequest.GetResponse()上 - I am getting WebException: “The operation has timed out” immediately on HttpWebRequest.GetResponse()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM