一、HTTP请求
1.请求行
Method Request-URI HTTP-Version CRLF
eg. GET /form.html http/1.1 (CRFL)
2.消息报头
请求报头:
名字+":"+空格+值
eg. Accept: text/html(表示客户端希望接受html文本)
3.请求正文
二、HTTP响应
1.状态行
HTTP-Version Status-Code Reason-phrase CRLF
eg. HTTP/1.1 200 OK (CRLF)
2.消息报头
响应报头:
允许服务器传递不能放在状态行的附加响应信息,以及关于服务器的信息和对Request-URI所标识的资源进行下一步的访问信息。
3.响应正文
三、常见HTTP响应状态码
1XX 信息响应
100 Continue This interim response indicates that everything so far is OK and that the client should continue with the request or ignore it if it is already finished.继续。客户端应继续其请求。
101 Switching Protocol This code is sent in response to an Upgrade request header by the client, and indicates the protocol the server is switching too.切换协议。服务器根据客户端的请求切换协议。只能切换到更高级的协议,例如,切换到HTTP的新版本协议
2XX 成功响应
200 OK 请求成功。
成功的含义取决于HTTP方法:
GET:资源已被提取并在消息正文中传输。
HEAD:实体标头位于消息正文中。
POST:描述动作结果的资源在消息体中传输。
TRACE:消息正文包含服务器收到的请求消息
201 Created 该请求已成功,并因此创建了一个新的资源。这通常是在PUT请求之后发送的响应。
202 Accepted 请求已经接收到,但还未响应,没有结果。意味着不会有一个异步的响应去表明当前请求的结果,预期另外的进程和服务去处理请求,或者批处理。
3XX 重定向
300 Multiple Choice 【特殊重定向】
The request has more than one possible responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses. 多种选择。请求的资源可包括多个位置,相应可返回一个资源特征与地址的列表用于用户终端(例如:浏览器)选择。【应用场景:不会太多,所有的选项在消息主体的 HTML 页面中列出。也可以返回 200 OK
状态码。】
301 Moved Permanently 【永久重定向】
This response code means that URI of requested resource has been changed. Probably, new URI would be given in the response.永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。今后任何新的请求都应使用新的URI代替。【应用场景:网站重构】
302 Found 【临时重定向】
This response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. Therefore, this same URI should be used by the client in future requests. 临时移动。与301类似。但资源只是临时被移动。客户端应继续使用原有URI。【应用场景:由于不可预见的原因该页面暂不可用。在这种情况下,搜索引擎不会更新它们的链接。】
303 See other 【临时重定向】
Server sent this response to directing client to get requested resource to another URI with an GET request. 查看其它地址。与301类似。使用GET和POST请求查看。【应用场景:其他方法店都会变更为GET
,用于put
或post
请求完成之后进行页面跳转来防止由于页面刷新导致的操作的重复触发。】
304 Not Modified 【特殊重定向】
This is used for caching purposes. It is telling to client that response has not been modified. So, client can continue to use same cached version of response. 未修改。所请求的资源未修改,服务器返回此状态码时,不会返回任何资源。客户端通常会缓存访问过的资源,通过提供一个头信息指出客户端希望只返回在指定日期之后修改的资源。【应用场景:缓存刷新,该状态码表示缓存值依然有效,可以使用】
4XX 客户端错误
400 Bad request This response means that server could not understand the request due to invalid syntax. 客户端请求的语法错误,服务器无法理解。
401 Unauthorized Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.请求要求用户的身份认证。
403 Forbidden The client does not have access rights to the content, i.e. they are unauthorized, so server is rejecting to give proper response. Unlike 401, the client's identity is known to the server.服务器理解请求客户端的请求,但是没有权限,拒绝执行此请求。
404 Not Found The server can not find requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurence on the web.服务器无法根据客户端的请求找到资源(网页)。通过此代码,网站设计人员可设置"您所请求的资源无法找到"的个性页面.
5XX 服务端错误
500 Internal Server Error The server has encountered a situation it doesn't know how to handle.服务器内部错误,无法完成请求。
501 Not Implemented The request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD.服务器不支持请求的功能,无法完成请求。
502 Bad GateWay This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。
503 Service Unavailable The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded.This responses should be used for temporary conditions and the Retry-After: HTTP header should, if possible, contain the estimated time before the recovery of the service.由于超载或系统维护,服务器暂时的无法处理客户端的请求。延时的长度可包含在服务器的Retry-After头信息中。
504 Gateway Timeout This error response is given when the server is acting as a gateway and cannot get a response in time.充当网关或代理的服务器,未及时从远端服务器获取请求。
505 HTTP Version not supported he HTTP version used in the request is not supported by the server.服务器不支持请求的HTTP协议的版本,无法完成处理。