## 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|
- JavaCook
- Java专题零:类的继承
- Java专题一:数据类型
- Java专题二:相等与比较
- Java专题三:集合
- Java专题四:异常
- Java专题五:遍历与迭代
- Java专题六:运算符
- Java专题七:正则表达式
- Java专题八:泛型
- Java专题九:反射
- Java专题九(1):反射
- Java专题九(2):动态代理
- Java专题十:日期与时间
- Java专题十一:IO与NIO
- Java专题十一(1):IO
- Java专题十一(2):NIO
- Java专题十二:网络
- Java专题十三:并发编程
- Java专题十三(1):线程与线程池
- Java专题十三(2):线程安全与同步
- Java专题十三(3):内存模型、volatile、ThreadLocal
- Java专题十四:JDBC
- Java专题十五:日志
- Java专题十六:定时任务
- Java专题十七:JavaMail
- Java专题十八:注解
- Java专题十九:浅拷贝与深拷贝
- Java专题二十:设计模式
- Java专题二十一:序列化与反序列化
- 附加专题一:MySQL
- MySQL专题零:简介
- MySQL专题一:安装与连接
- MySQL专题二:DDL与DML语法
- MySQL专题三:工作原理
- MySQL专题四:InnoDB存储引擎
- MySQL专题五:sql优化
- MySQL专题六:数据类型
- 附加专题二:Mybatis
- Mybatis专题零:简介
- Mybatis专题一:配置文件
- Mybatis专题二:映射文件
- Mybatis专题三:动态SQL
- Mybatis专题四:源码解析
- 附加专题三:Web编程
- Web专题零:HTTP协议
- Web专题一:Servlet
- Web专题二:Cookie与Session
- 附加专题四:Redis
- Redis专题一:数据类型
- Redis专题二:事务
- Redis专题三:key的过期
- Redis专题四:消息队列
- Redis专题五:持久化