HTTP协议状态码大全

笔者 出处:https://blog.csdn.net/JackMengJin 笔者原创,文章转载需注明,如果喜欢请点赞+关注,感谢支持!

 

写在前面:作为一位网络从业者,对于网络协议的学习的过程可谓是入门即碰壁,开始时就被一座大山堵在门口无法前进。

为什么这么说呢?网络协议可谓是非常抽象的概念:从OSI七层模型,到TCP/IP协议簇物理层协议、数据链路层协议、网络传输层协议、应用层协议等等无数个网络协议伴随着无数个RFC规范,想完全弄懂并掌握它们的工作流程和实现原理,这个难度并不是一般人能够轻易攻克的。

但话又说回来,越是上层的、应用层的网络协议越是更贴近生活,越是好理解。

举个例子:让一个零基础的软件工程师去理解OSPF协议,可能他一周也说不出个所以然。

但HTTP协议不一样,对于HTTP协议这类应用层的网络协议来说,随处可见,会电脑的人机绘都会用。

想去彻底搞懂HTTP协议,彻底弄懂它,就要从协议基础、也就是从规范细节搞定他。

下面就开启HTTP协议的学习之路,首先从HTTP状态码开始。A path to pain.


 

目录

HTTP协议状态码最强攻略

一、HTTP基础知识

1.1 HTTP协议是什么?

1.2 HTTP版本

1.3 简单解析HTTP协议

 

二、HTTP状态码

2.1 什么是状态码?

2.2 状态码的作用

2.3 1xx

2.4 2xx

2.5 3xx

2.6 4xx

2.7 5xx


 

HTTP协议状态码最强攻略

由于HTTP协议内容篇幅较多,在HTTP协议状态码的学习攻略之前,先简单的说下HTTP协议的相关内容,仅点到为止。

一、HTTP基础知识

1.1 HTTP协议是什么?

  • HTTP英文是Hyper Text Transfer Protocol(超文本传输协议)
  • 它是一个简单的请求-响应协议,是基于C/S架构进行通信,通常运行在TCP之上。
  • HTTP于1990年提出,是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。

1.2 HTTP版本

  • HTTP1.0

HTTP1.1之前的版本由于太过久远,这里不再讨论,直接进入HTTP1.1之后的版本学习。

在1.0协议中,双方规定了连接方式和连接类型,这已经极大扩展了HTTP的领域,但对于互联网最重要的速度和效率,并没有太多的考虑。毕竟,作为协议的制定者,当时也没有想到HTTP协议会有那么快的普及速度。

HTTP1.1协议的具体内容可以参考RFC 2616,这里附上RFC2616的链接 ->https://tools.ietf.org/html/rfc2616

这里需要说明的是,如果你有精力,有时间并英语不错的情况下,强烈推荐通读RFC相关文献,这对你学习网络协议来说是质的飞跃。

  • HTTP2.0

HTTP2.0的前世是HTTP1.0和HTTP1.1。虽然之前仅仅只有两个版本,但这两个版本所包含的协议规范之庞大,足以让任何一个有经验的工程师为之头疼。网络协议新版本并不会马上取代旧版本。实际上,1.0和1.1在之后很长的一段时间内一直并存,这是由于网络基础设施更新缓慢所决定的。

关于HTTP2.0协议的具体内容可以参考RFC 7540。这里附上RFC 7540的链接 ->https://tools.ietf.org/html/rfc7540

各个版本之间的差异不是本文重点,点到为止。

1.3 简单解析HTTP协议

打开浏览器访问www.qq.com,并同时通过wireshark抓包,查看HTTP协议:

通过抓包可以看到,HTTP协议是在TCP协议的下面,所以这也是为什么说http协议是运行在TCP之上。

通过抓包可以看到报文字段的各个内容:

可以看到这是request报文,方式是GET方法,同时版本为HTTP1.1版本,等等一系列的信息。

而这个报文就是HTTP之请求消息Request报文

那除了Request报文还有别的报文类型么?

当然有,HTTP之响应消息Response报文。同样可以通过抓包看到Response报文的各个字段。

箭头指向的字段 Status Code:200,这就是文本的主角,也就是HTTP状态码

由于本文主要去讲解HTTP状态码相关内容,更多HTTP协议的知识请持续关注我的博客,后续会逐步一一进行讲解。


 

二、HTTP状态码

OK,终于到了文本的重头戏——Status Code

2.1 什么是状态码?

HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码。它由 RFC 2616 规范定义的,并得到 RFC 2518、RFC 2817、RFC 2295、RFC 2774 与 RFC 4918 等规范扩展。所有状态码的第一个数字代表了响应的五种状态之一。所示的消息短语是典型的,但是可以提供任何可读取的替代方案。 除非另有说明,状态码是HTTP / 1.1标准(RFC 7231)的一部分。

也就是说HTTP状态码其实就是响应状态的3位数字代码。

2.2 状态码的作用

作用是什么很明显,就是响应状态的一种说明,通过3位数字去表示。

这里很多面试官会去问:你知道哪些HTTP状态码?

其实常用的就那几个:

  • 200 - 请求成功
  • 301 - 资源(网页等)被永久转移到其它URL
  • 404 - 请求的资源(网页等)不存在
  • 500 - 内部服务器错误

而通过RFC2616的内容可以得知还有很多很多状态码是不怎么常用的:

目前状态码远远不止RFC2616上面的内容,作为《HTTP协议状态码最强攻略》,这里会把所有的状态码统统写在下面。

 


2.3 1xx

1xx表示消息类。1xx这一类型的状态码,代表请求已被接受,需要继续处理。

Informational 1xx

   This class of status code indicates a provisional response,
   consisting only of the Status-Line and optional headers, and is
   terminated by an empty line. There are no required headers for this
   class of status code. Since HTTP/1.0 did not define any 1xx status
   codes, servers MUST NOT send a 1xx response to an HTTP/1.0 client
   except under experimental conditions.

   A client MUST be prepared to accept one or more 1xx status responses
   prior to a regular response, even if the client does not expect a 100
   (Continue) status message. Unexpected 1xx status responses MAY be
   ignored by a user agent.

   Proxies MUST forward 1xx responses, unless the connection between the
   proxy and its client has been closed, or unless the proxy itself
   requested the generation of the 1xx response. (For example, if a

   proxy adds a "Expect: 100-continue" field when it forwards a request,
   then it need not forward the corresponding 100 (Continue)
   response(s).)

这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束。

由于 HTTP/1.0 协议中没有定义任何 1xx 状态码,所以除非在某些试验条件下,服务器禁止向此类客户端发送 1xx 响应。

  • 100 Continue

定义:收到部分请求,请求仍在继续。

100 Continue

   The client SHOULD continue with its request. This interim response is
   used to inform the client that the initial part of the request has
   been received and has not yet been rejected by the server. The client
   SHOULD continue by sending the remainder of the request or, if the
   request has already been completed, ignore this response. The server
   MUST send a final response after the request has been completed. See
   section 8.2.3 for detailed discussion of the use and handling of this
   status code.

客户端应该继续它的请求。这个临时反应是用于通知客户端请求的初始部分有已收到,且尚未被服务器拒绝。客户端应该继续发送请求的其余部分,如果请求已经完成,请忽略此响应。服务器必须在请求完成后发送最后的响应。

  • 101 Switching Protocols

定义:服务器转换协议

101 Switching Protocols

   The server understands and is willing to comply with the client's
   request, via the Upgrade message header field (section 14.42), for a
   change in the application protocol being used on this connection. The
   server will switch protocols to those defined by the response's
   Upgrade header field immediately after the empty line which
   terminates the 101 response.

   The protocol SHOULD be switched only when it is advantageous to do
   so. For example, switching to a newer version of HTTP is advantageous
   over older versions, and switching to a real-time, synchronous
   protocol might be advantageous when delivering resources that use such 
   features.

服务器理解并愿意遵守客户的要求通过升级消息头字段请求更改此连接上使用的应用程序协议。只有在对协议有利的情况下,才应该切换协议。

 


2.4 2xx

这类状态代码表示客户机的请求是成功接收、理解、并接受。通俗来说2开头的都是代表成功

  • 200 OK

定义:请求成功。

200 OK

   The request has succeeded. The information returned with the response
   is dependent on the method used in the request, for example:

   GET    an entity corresponding to the requested resource is sent in
          the response;

   HEAD   the entity-header fields corresponding to the requested
          resource are sent in the response without any message-body;

   POST   an entity describing or containing the result of the action;

   TRACE  an entity containing the request message as received by the
          end server.

请求成功。随响应返回的信息是否依赖于请求中使用的方法,比如GET、HEAD、POST、TRACE这些。

200就是成功,出现此状态码是表示正常状态。而上面举的例子里状态码就是200:

 

  • 201 Created

定义:表示某一资源创建成功,比如注册,和我现在正在发表博客,这些都是创建。

201 Created

   The request has been fulfilled and resulted in a new resource being
   created. The newly created resource can be referenced by the URI(s)
   returned in the entity of the response, with the most specific URI
   for the resource given by a Location header field. The response
   SHOULD include an entity containing a list of resource
   characteristics and location(s) from which the user or user agent can
   choose the one most appropriate. The entity format is specified by
   the media type given in the Content-Type header field. The origin
   server MUST create the resource before returning the 201 status code.
   If the action cannot be carried out immediately, the server SHOULD
   respond with 202 (Accepted) response instead.

   A 201 response MAY contain an ETag response header field indicating
   the current value of the entity tag for the requested variant just
   created, see section 14.19.
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值