table of Contents
Those in the wrong HTTP header vision
3.Accept-Encoding&Content-Encoding
HTTP header Overview
This article focuses on the packet header field ( header Field) , the HTTP header field is composed of field names and header field values, by a colon ":" separated, the following examples
Content-Type: application/xml
Connection: Keep-alive
Header fields ( header Field,) in accordance with the dimensions of the consumer can be divided into the following four categories, the specific details of a header reference documents and rfc2616
Header field classification | meaning | Common header |
---|---|---|
General General | Requests and responses both sides use |
Cache-Control、Connection、Date、Pragma、Trailer、Transfer-Encoding、Upgrade、Via、Warning |
Universal Entity Entity | Request and response entity body portion using |
Allow Content-Encoding、Content-Language、Content-Length、Content-Location、Content-MD5、Content-Range、Content-Type Expires、Last-Modified |
Request Request | 客户端发送请求时使用 |
Accept、Accept-Charset、Accept-Encoding、Accept-Language Authorization、Expect、From、Host If-Match、If-Modified-Since、If-None-Match、If-Range、If-Unmodified-Since Max-Forwards、Proxy-Authorization、Range、Referer、TE、User-Agent |
响应 Response | 服务端返回响应时使用 |
Accept-Ranges、Age、ETag、Location、Proxy-Authenticate、Retry-After、 Server、Vary、WWW-Authenticate |
还有Cookie、Set-Cookie和Content-Disposition等在其他RFC中定义的首部字段,它们的使用频率也很高。
按照功能分类,蓝色表请求头,紫色表示响应头,红色表示通用头
分类 | headers | 应用 |
---|---|---|
缓存控制 |
|
缓存控制参考 |
内容协商 | Accept、Accept-Charset、Accept-Encoding、Accept-Language Content-Encoding、Content-Language、Content-Length、Content-Location、Content-MD5、Content-Range、Content-Type |
某一资源,服务端有多个版本, 客户端告知服务器自己的偏好Accept, 服务器据偏好选择对应版本响应客户端的请求 |
连接管理 | Connection Keep-Alive |
Keep-Alive复用tcp连接 close 每次重新建立连接 |
代理路径 |
Via Max-Forwards |
Via: 1.1 ID-5301755340307730 uproxy-6 (http协议版本,代理服务器信息) Max-Forwards每经过跳-1, 0时代理服务器不能再转发该请求了 |
条件请求 | If-Match、If-None-Match、Etag If-Modified-Since、If-Unmodified-Since If-Range |
If-Match的值跟服务端ETag值匹配一致时, 服务端才会接受请求 If-Modified-Since 客户端缓存更新,如果在If-Modified-Since字段指定的日期时间后,资源发生了更新,服务器会接受请求 |
那些年用错HTTP header的异象
1.Content-length
字段Content-Length表明了实体主体部分的大小(单位是字节)。对实体主体进行内容编码传输时,不能再使用Content-Length首部字段。
项目中使用了Content-length字段,传输了报文体的字节长度,当时对端收到的报文少了一些字符。因为Content-length小于实际报文长度,导致发生截断。
解决方案,去掉该头即可。具体请戳
2.Content-Type
当传输有交换格式的报文,如xml等不带header(Content-Type:application/xml;charset=utf-8)的话,一般的http client实现发送时底层会对报文进行URLencode,对端收到的就是对特殊字符编码后的样子。
client传输的报文 | Server收到的报文 |
---|---|
<?xml version="1.0" encoding="UTF-8"?> | %3c%3fxml+version%3d%221.0%22+encoding%3d%22UTF-8%22%3f%3e |
3.Accept-Encoding&Content-Encoding
客户端头部Accept-Encoding字段值为空,则只有“ identity”编码是可以接受的。
当客户端传输Accept-Encoding: gzip时,服务端会传输gzip压缩后的响应,但是客户端如果不支持gzip解码的话,还按照简单的字节流处理方式,会报异常报文。
4.Connection
管理持久连接,http/1.1不传默认好似Keep-Alive。
通用头部header | 含义 |
---|---|
Connection: close | 当服务器端想明确断开连接时,则指定Connection首部字段的值为Close HTTP/1.1之前的HTTP版本的默认 |
Connection:Keep-Alive | 在旧版本的HTTP协议上维持持续连接 HTTP/1.1版本的默认连接都是持久连接 |
HTTP/1.1版本的默认连接都是持久连接。为此,客户端会在持久连接上连续发送请求。当服务器端想明确断开连接时,则指定Connection首部字段的值为Close。
没有配置这个字段,默认是Keep-Alive,客户端会复用这个tcp连接,如果服务端断开了连接,就会报通信异常,对端已经关闭了连接。
可以通过传输Connection: close 主动关闭连接(不再复用),避免后续请求的通信异常。