简体   繁体   English

C#HttpWebRequest.GetResponse - 如何处理非异常与webexception响应的StatusCode用法?

[英]C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response?

Can someone help clear up the usage of the "StatusCode" property in HttpWebResponse and WebException? 有人可以帮助清除HttpWebResponse和WebException中“StatusCode”属性的用法吗?

For example it seems that if: 例如,似乎如果:

a) there is no exception, then the HttpWebResponse will have a StatusCode that could have some values that indicate both: - success (eg OK, Accepted etc) - failure (eg UseProxy, RequestTimeout etc) a)没有异常,那么HttpWebResponse将有一个StatusCode可以有一些值,表明两者: - 成功(例如OK,Accepted等) - 失败(例如UseProxy,RequestTimeout等)

b) there is a WebExeption throw, which itself has a response object that again has a StatusCode (which I assume is based on the same HttpStatusCode Enumeration. b)有一个WebExeption throw,它本身有一个响应对象,它又有一个StatusCode(我假设它是基于相同的HttpStatusCode枚举)。

Question 1 - Is there any consistency in terms of what StatusCode's will trigger a WebException (and you'd pick up the detail within the exception), versus which would come back without an exception but you'd find out the result in the StatusCode of the response object? 问题1 - 在StatusCode将触发WebException(并且您在异常中获取详细信息)方面是否存在任何一致性,相比之下,没有异常会返回但是您会在StatusCode中找到结果响应对象?

Question 2 - Or more specifically what is the pseduo code (or C# code itself) for trying to handle a httpWebRequest.GetResponse call such that you want to differentiate between the categories of responses for the user: 问题2 - 或者更具体地说,尝试处理httpWebRequest.GetResponse调用的pseduo代码(或C#代码本身)是什么,以便您想要区分用户的响应类别:

  • proxy settings / proxy issue => so can tell user to fix proxy settings 代理设置/代理问题=>所以可以告诉用户修复代理设置

  • connectivity issue / web-server down => so user is aware of this 连接问题/ web-server down =>所以用户意识到这一点

  • server side error (eg server is there but there is an issue handling the request - eg content not there) => so user can raise with website manager 服务器端错误(例如服务器在那里,但处理请求有问题 - 例如内容不在那里)=>所以用户可以提出网站管理员

  • success case (and I assume this would be more than just the OK) => na (success case) 成功案例(我认为这不仅仅是OK)=> na(成功案例)

thanks 谢谢

根据我的经验,响应状态代码仅返回200或0.其他任何内容都来自WebException,包括代理错误,如407或417。

The WebException is thrown whenever the web request cannot be executed successfully. 只要无法成功执行Web请求,就会抛出WebException。 For eg 400 and 500 series of responses. 例如400和500系列的回复。

WebExcpetion has a property named Status which will return the actual status of the response ie 500 (Internal Server Error). WebExcpetion有一个名为Status的属性,它将返回响应的实际状态,即500(内部服务器错误)。

Here is the list of all response codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 以下是所有回复代码的列表: http//www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

=============================================================================== ================================================== =============================

In general: 一般来说:

1xx series of code = provisional response. 1xx系列代码=临时响应。 These are not error codes. 这些不是错误代码。 For eg the 100 Continue response which tells that client should continue with its request. 例如,100 Continue响应,告知客户端应继续其请求。 Usually WebRequest will not return such response, and handle it itself by sending the rest of request. 通常,WebRequest不会返回此类响应,并通过发送其余请求来自行处理。

2xx series of code = Request was successful received, understood and accepted. 2xx系列代码=请求已成功接收,理解和接受。 These are not error codes. 这些不是错误代码。 For eg 200 OK 例如200 OK

3xx series of code = Further action needs to be taken. 3xx系列代码=需要采取进一步行动。 Generally this is not error code (usually its for re-direction) for eg '301 Moved Permanently', which means that the resource being request is moved to a new location, so any further requests by the client should be on the new URL provided in the response. 通常这不是错误代码(通常用于重定向),例如'301 Moved Permanently',这意味着正在请求的资源被移动到新位置,因此客户端的任何进一步请求应该在提供的新URL上在回应中。

OR '305 Use Proxy', which according to you results in an Exception. 或'305使用代理',根据您导致异常。

4xx series of code = Client errors. 4xx系列代码=客户端错误。 These can result in exception. 这些可能导致异常。 for eg '400 Bad Request' or '401 Unauthorized' 例如'400 Bad Request'或'401 Unauthorized'

5xx series of code = Server errors. 5xx系列代码=服务器错误。 These can result in exception. 这些可能导致异常。 for eg '500 Internal Server Error' or '504 Gateway Timeout' 例如'500 Internal Server Error'或'504 Gateway Timeout'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpWebRequest.GetResponse(),是否除了200之外的每个StatusCode都会引发异常? - HttpWebRequest.GetResponse() , does every StatusCode besides 200 throws exception? C#HttpWebRequest.GetResponse编码URL - C# HttpWebRequest.GetResponse encoding URL 如何知道Response的长度(HttpWebRequest.GetResponse()。GetResponseStream()) - How to know the length of Response (HttpWebRequest.GetResponse().GetResponseStream()) C#HttpWebRequest.GetResponse()返回错误:(404)找不到 - c# HttpWebRequest.GetResponse() returned an error: (404) Not Found 哪些特定的状态代码会导致HttpWebRequest.GetResponse()抛出WebException? - Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()? HttpWebRequest.GetResponse方法抛出404异常 - HttpWebRequest.GetResponse methods throws 404 exception 当HttpWebRequest.GetResponse抛出WebException时,如何读取返回的自定义错误消息? - How do I read a custom error message returned when HttpWebRequest.GetResponse throws a WebException? HTTPWebRequest.GetResponse()抛出连接失败异常 - HTTPWebRequest.GetResponse() throws connection failure exception HttpWebRequest.GetResponse()上的请求异常过多 - Too many requests exception on HttpWebRequest.GetResponse() HttpWebRequest.GetResponse() 与 GetResponseAsync() 中的超时行为 - Timeout behaviour in HttpWebRequest.GetResponse() vs GetResponseAsync()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM