【已更新最新开发文章,点击查看详细】

HttpWebResponse.StatusCode 属性获取响应的状态。对应 HttpStatusCode 枚举值之一。

HttpStatusCode 枚举
Accepted 202

Accepted 指示已接受请求做进一步处理。

Ambiguous 300

Ambiguous 指示请求的信息有多种表示形式。 

MultipleChoices。

BadGateway 502

BadGateway 指示中间代理服务器从另一代理或原始服务器接收到错误响应。

BadRequest 400

如果没有其他适用的错误,

BadRequest。

Conflict 409

Conflict 指示由于服务器上的冲突而未能执行请求。

Continue 100

Continue 指示客户端可能继续其请求。

Created 201

Created 指示请求导致在响应被发送前创建新资源。

ExpectationFailed 417

ExpectationFailed 指示服务器未能符合 Expect 标头中给定的预期值。

Forbidden 403

Forbidden 指示服务器拒绝满足请求。

Found 302

Found 指示请求的信息位于 Location 标头中指定的 URI 处。 

原始请求方法为 POST 时,

重定向。

GatewayTimeout 504

GatewayTimeout 指示中间代理服务器在等待来自另一个代理或原始服务器的响应时已超时。

Gone 410

Gone 指示请求的资源不再可用。

HttpVersionNotSupported 505

HttpVersionNotSupported指示服务器不支持请求的 HTTP 版本。

InternalServerError 500

InternalServerError 指示服务器上发生了一般错误。

LengthRequired 411

LengthRequired 指示缺少必需的 Content-length 标头。

MethodNotAllowed 405

MethodNotAllowed 指示请求的资源上不允许请求方法(POST 或 GET)。

Moved 301

Moved 指示请求的信息已移到 Location 头中指定的 URI 处。 

原始请求方法为 POST 时,

MovedPermanently。

MovedPermanently 301

MovedPermanently 指示请求的信息已移到 Location 头中指定的 URI 处。 

Moved。

MultipleChoices 300

默认操作是将此状态视为重定向,

不明确。

NoContent 204

NoContent 指示已成功处理请求并且响应已被设定为无内容。

NonAuthoritativeInformation 203

NonAuthoritativeInformation 指示返回的元信息来自缓存副本而不是原始服务器,

因此可能不正确。

NotAcceptable 406

NotAcceptable 指示客户端已用 Accept 标头指示将不接受资源的任何可用表示形式。

NotFound 404

NotFound 指示请求的资源不在服务器上。

NotImplemented 501

NotImplemented 指示服务器不支持请求的函数。

NotModified 304

未传输此资源的内容。

OK 200

这是最常接收的状态代码。

PartialContent 206

PartialContent 指示响应是包括字节范围的 GET 请求所请求的部分响应。

PaymentRequired 402

PaymentRequired 以供将来使用。

PreconditionFailed 412

PreconditionFailed 指示一个条件设置为此请求失败,并且无法执行请求。

使用条件请求标头,如: If-match、 设置条件 If-None-匹配项,或如果-以未修改形式-自。

ProxyAuthenticationRequired 407

ProxyAuthenticationRequired 指示请求的代理要求身份验证。 

Proxy-authenticate 标头包含如何执行身份验证的详细信息。

Redirect 302

Redirect 指示请求的信息位于 Location 标头中指定的 URI 处。

原始请求方法为 POST 时,重定向的请求将使用 GET 方法。 

找到。

RedirectKeepVerb 307

RedirectKeepVerb 指示请求信息位于 Location 标头中指定的 URI 处。 

TemporaryRedirect。

RedirectMethod 303

RedirectMethod 将客户端自动重定向到 Location 标头中指定的 URI。 

SeeOther。

RequestedRangeNotSatisfiable 416

RequestedRangeNotSatisfiable 指示无法返回从资源请求的数据范围,因为范围的开头在资源的开头之前,

或因为范围的结尾在资源的结尾之后。

RequestEntityTooLarge 413

RequestEntityTooLarge 指示请求太大,服务器无法处理。

RequestTimeout 408

RequestTimeout 指示客户端没有在服务器期望请求的时间内发送请求。

RequestUriTooLong 414

RequestUriTooLong 指示 URI 太长。

ResetContent 205

ResetContent 指示客户端应重置(而非重新加载)当前资源。

SeeOther 303

SeeOther 将客户端自动重定向到 Location 标头中指定的 URI。

RedirectMethod

ServiceUnavailable 503

ServiceUnavailable 指示服务器暂时不可用,通常是由于过多加载或维护。

SwitchingProtocols 101

SwitchingProtocols 指示正在更改协议版本或协议。

TemporaryRedirect 307

TemporaryRedirect 指示请求信息位于 Location 标头中指定的 URI 处。 

RedirectKeepVerb。

Unauthorized 401

WWW-Authenticate 标头包含如何执行身份验证的详细信息。

UnsupportedMediaType 415

UnsupportedMediaType指示请求是不受支持的类型。

Unused 306

Unused 是未完全指定的 HTTP/1.1 规范的建议扩展。

UpgradeRequired 426

UpgradeRequired 指示客户端应切换为诸如 TLS/1.0 之类的其他协议。

UseProxy 305

UseProxy 指示请求应使用位于 Location 标头中指定的 URI 的代理服务器。

 
示例
下面的示例将返回的状态进行比较HttpWebResponse的成员HttpStatusCode类来确定响应的状态。
 1 HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com");
 2 httpReq.AllowAutoRedirect = false;
 3 
 4 HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
 5 
 6 if (httpRes.StatusCode==HttpStatusCode.Moved) 
 7 {
 8     // ToDo 
 9 }
10 
11 // 关闭响应
12 httpRes.Close();
注解

HttpWebResponse.StatusCode 属性。

HttpWebRequest.AllowAutoRedirect属性是false,下面的枚举值会导致引发异常:

    • Ambiguous
    • Found
    • MultipleChoices
    • Redirect
    • RedirectKeepVerb
    • RedirectMethod
    • SeeOther
    • TemporaryRedirect

 

【已更新最新开发文章,点击查看详细】

相关文章:

  • 2021-06-02
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-12-01
  • 2021-09-03
  • 2022-01-25
猜你喜欢
  • 2021-12-26
  • 2021-08-08
  • 2021-05-30
  • 2021-06-30
  • 2021-06-21
  • 2021-05-16
相关资源
相似解决方案