多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## Web专题零:HTTP协议 [TOC] ### 1. Request请求 ~~~ Request = Request-Line *(( general-header | request-header | entity-header ) CRLF) CRLF [ message-body ] ~~~ 请求Message的第一行是固定的请求行,如:`GET /rfc/rfc2616.txt HTTP/1.1 ` ~~~ Request-Line = Method SP Request-URI SP HTTP-Version CRLF ~~~ - Method 请求方法包括下面7种: `OPTIONS`,`GET`,`HEAD`,`POST`,`PUT`,`DELETE`,`TRACE`,`CONNECT` - Request-URI 表示资源的路径 - HTTP-Version HTTP协议版本一般都是HTTP/1.1 ### 2. Response响应 ~~~ Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] ~~~ 响应Message的第一行是固定的状态行,如:`HTTP/1.1 200 OK` ~~~ Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF ~~~ ### 3. Header 字段 #### 3.1. Accept > 用于指定某些媒体可接受的响应类型 语法: ~~~ Accept = "Accept" ":" #( media-range [ accept-params ] ) media-range = ( "*/*" | ( type "/" "*" ) | ( type "/" subtype ) ) *( ";" parameter ) accept-params = ";" "q" "=" qvalue *( accept-extension ) accept-extension = ";" token [ "=" ( token | quoted-string ) ] ~~~ 例子: ~~~ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 ~~~ #### 3.2. Cache-Control > 用于指定指令在请求/响应链上的所有缓存机制必须遵守的约定,表明是否缓存,缓存时间等 语法: ~~~ Cache-Control = "Cache-Control" ":" 1#cache-directive cache-directive = cache-request-directive | cache-response-directive cache-request-directive = "no-cache" ; | "no-store" ; | "max-age" "=" delta-seconds ; | "max-stale" [ "=" delta-seconds ] ; | "min-fresh" "=" delta-seconds ; | "no-transform" ; | "only-if-cached" ; | cache-extension ; cache-response-directive = "public" ; | "private" [ "=" <"> 1#field-name <"> ] ; | "no-cache" [ "=" <"> 1#field-name <"> ]; | "no-store" ; | "no-transform" ; | "must-revalidate" ; | "proxy-revalidate" ; | "max-age" "=" delta-seconds ; | "s-maxage" "=" delta-seconds ; | cache-extension ; cache-extension = token [ "=" ( token | quoted-string ) ] ~~~ 例子: - Request头部 ~~~ Cache-Control: max-age=0 ~~~ - Response头部 ~~~ Cache-Control: max-age=3600 ~~~ #### 3.3. Expires > 用于提供日期/时间,在这之后的响应被认为是过时的; > 如果response中包含max-age的Cache-Control字段,则max-age的优先级比Expires的优先级高 语法: ~~~ Expires = "Expires" ":" HTTP-date ~~~ 例子: ~~~ Expires: Tue, 21 Apr 2020 09:50:25 GMT ~~~ #### 3.4. Date > 用于表示消息发出的日期时间,通常在Response响应头中必须包含Date字段,除了Response响应status是100(Continue)、 101(Switching Protocols)、500(Internal Server Error)、503(Service Unavailable)等,或者Server没有一个时钟用来生成合理准确的日期时间等情况 语法: ~~~ Date = "Date" ":" HTTP-date ~~~ 例子: ~~~ Date: Tue, 21 Apr 2020 09:32:18 GMT ~~~ #### 3.5. Connection > 用于指定发送者与服务器连接的需要的选项 语法: ~~~ Connection = "Connection" ":" 1#(connection-token) connection-token = token ~~~ 例子: - 支持持久连接 ~~~ Connection: keep-alive ~~~ - 不支持持久连接,连接完成后关闭连接 ~~~ Connection: close ~~~ #### 3.6. User-Agent > 用于代理用户完成request,通常的user-agent是浏览器; 语法: ~~~ User-Agent = "User-Agent" ":" 1*( product | comment ) ~~~ 例子: ~~~ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36 ~~~ #### 3.7. Server > 用于表明响应request的服务器的信息 语法: ~~~ Server = "Server" ":" 1*( product | comment ) ~~~ 例子: ~~~ Server: Apache/2.4.18 (Ubuntu) ~~~ #### 3.8. Last-Modified > 用于表明最后修改的时间 > 对于文件系统,是文件最后修改的时间 > 对于数据库系统,是最后一次更新记录的时间戳 语法: ~~~ Last-Modified = "Last-Modified" ":" HTTP-date ~~~ 例子: ~~~ Last-Modified: Fri, 11 Jun 1999 18:46:53 GMT ~~~ #### 3.9. Status > 用于表示请求后的状态,用3位整数表示 | Item | Status | Description | | :------------ | :------------ | :------------ | | Informational | 100 | Continue | | Informational | 101 | Switching Protocols | | Successful | 200 | OK | | Successful | 201 | Created | | Successful | 202 | Accepted | | Successful | 203 | Non-Authoritative Information | | Successful | 204 | No Content | | Successful | 205 | Reset Content | | Successful | 206 | Partial Content | | Redirection | 300 |Multiple Choices | | Redirection | 301 | Moved Permanently| | Redirection | 302 |Found | | Redirection | 303 |See Other | | Redirection | 304 |Not Modified | | Redirection | 305 | Use Proxy| | Redirection | 306 |(Unused) | | Redirection | 307 |Temporary Redirect | | Client Error | 400 | Bad Request| | Client Error | 401 | Unauthorized| | Client Error | 402 | Payment Required| | Client Error | 403 | Forbidden| | Client Error | 404 | Not Found | | Client Error | 405 |Method Not Allowed | | Client Error | 406 | Not Acceptable| | Client Error | 407 |Proxy Authentication Required | | Client Error | 408 | Request Timeout | | Client Error | 409 |Conflict | | Client Error | 410 | Gone| | Client Error | 411 | Length Required| | Client Error | 412 | Precondition Failed| | Client Error | 413 |Request Entity Too Large | | Client Error | 414 |Request-URI Too Long| | Client Error | 415 | Unsupported Media Type | | Client Error | 416 | Requested Range Not Satisfiable | | Client Error | 417 |Expectation Failed| | Server Error| 500 |Internal Server Error| | Server Error| 501 |Not Implemented| | Server Error| 502 |Bad Gateway | | Server Error| 503 |Service Unavailable| | Server Error| 504 |Gateway Timeout| | Server Error| 505 |HTTP Version Not Supported|