C#HttpWebRequest.GetResponse - 如何使用的StatusCode对非例外VS引发WebException响应处理? [英] C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response?

查看:552
本文介绍了C#HttpWebRequest.GetResponse - 如何使用的StatusCode对非例外VS引发WebException响应处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮助清理的StatusCode财产的使用情况HttpWebResponse和引发WebException?

Can someone help clear up the usage of the "StatusCode" property in HttpWebResponse and WebException?

例如看来,如果:

一)没有异常,那么HttpWebResponse将有的StatusCode,可能有一些值,表明这两个:
- 成功(如OK,接受等)
- 故障(如UseProxy,将requestTimeout等)

a) there is no exception, then the HttpWebResponse will have a StatusCode that could have some values that indicate both: - success (e.g. OK, Accepted etc) - failure (e.g. UseProxy, RequestTimeout etc)

b)有一个WebExeption扔,它本身有一次有一个的StatusCode(我假设是基于相同的HttpStatusCode枚举响应对象。

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.

问题1
- 是否有什么样的StatusCode的将触发引发WebException(和你拿起例外中的细节),与该会回来没有例外,但你最好条款有任何一致性查找出来的结果在响应对象的StatusCode

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?

问题2 - 或者更确切地说是什么pseduo代码(或C#代码本身),用于试图处理一个httpWebRequest.GetResponse呼叫,使得要为用户的响应的类别来区分:

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:


  • 代理服务器设置/代理发行
    =>这样可以告诉用户修复代理设置

  • proxy settings / proxy issue => so can tell user to fix proxy settings

连接问题/ web服务器下跌
=>所以用户也知道这一点

connectivity issue / web-server down => so user is aware of this

服务器端错误(例如,服务器是有,但没有处理请求的问题 - 如内容不存在)
=>让用户可以与网站管理员提高

server side error (e.g. server is there but there is an issue handling the request - e.g 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)

感谢

推荐答案

每当Web请求不能成功执行的引发WebException异常。对于如400和500系列的响应。

The WebException is thrown whenever the web request cannot be executed successfully. For e.g 400 and 500 series of responses.

WebExcpetion有一个属性名为状态将返回,即500(内部服务器错误)响应的实际状态。

WebExcpetion has a property named Status which will return the actual status of the response i.e 500 (Internal Server Error).

下面是所有响应代码列表:的 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Here is the list of all response codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

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

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

在一般的:

1XX系列代号=临时反应。这些都不是错误代码。对于例如100继续响应它告诉客户端应该继续其请求。通常的WebRequest将不会返回响应等,并通过发送请求的其余部分处理它本身。

1xx series of code = provisional response. These are not error codes. For e.g the 100 Continue response which tells that client should continue with its request. Usually WebRequest will not return such response, and handle it itself by sending the rest of request.

2xx序列码=请求被成功接收,理解和接受。这些都不是错误代码。对于例如200 OK

2xx series of code = Request was successful received, understood and accepted. These are not error codes. For e.g 200 OK

3XX系列代码=进一步行动需要采取。通常这不是错误代码(通常其用于重新方向)为例如'301永久移动,这意味着,该资源被请求被移动到一个新的位置,以便由客户端的任何进一步的请求应该对所提供的新的URL在响应中。

3xx series of code = Further action needs to be taken. Generally this is not error code (usually its for re-direction) for e.g '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.

OR'305使用代理服务器,它根据您的结果异常。

OR '305 Use Proxy', which according to you results in an Exception.

4XX系列码=客户端的错误。这些可导致异常。对于例如400错误的请求或401未授权

4xx series of code = Client errors. These can result in exception. for e.g '400 Bad Request' or '401 Unauthorized'

5XX系列代号=服务器错误。这些可导致异常。对于例如500内部服务器错误或504网关超时

5xx series of code = Server errors. These can result in exception. for e.g '500 Internal Server Error' or '504 Gateway Timeout'

这篇关于C#HttpWebRequest.GetResponse - 如何使用的StatusCode对非例外VS引发WebException响应处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆