HTTP头各字段详解

[toc]

记录下http头各个字段的含义。

Accept

浏览器(或者其他基于HTTP的客户端程序)可以接收的内容类型(或者说通知服务器可以发回的数据类型),例如 Accept: text/plain

示例:

Accept: text/html
Accept: image/*
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8

Accept-Charset

浏览器能识别的字符集

示例:

Accept-Charset: iso-8859-1
Accept-Charset: utf-8, iso-8859-1;q=0.5
Accept-Charset: utf-8, iso-8859-1;q=0.5, *;q=0.1

Accept-Encoding

浏览器可以处理的编码方式,这里的编码方式通常指gzip,deflate等

Accept-Encoding: gzip
Accept-Encoding: compress
Accept-Encoding: deflate
Accept-Encoding: br
Accept-Encoding: identity
Accept-Encoding: *
// Multiple algorithms, weighted with the quality value syntax:
Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5

Accept-Encoding: gzip, deflate
//浏览器告诉服务器支持 gzip 和 deflate 两种数据格式,服务器收到这种请求之后,会进行 gzip 或 deflate 压缩(一般都是返回 gzip 格式的数据)。

Accept-Language

浏览器接收的语言,其实也就是用户在什么语言地区

Accept-Language: zh-CN

Authorization

包含用于向服务器验证用户代理身份的凭据。

Cache-Control

在request和response中都有,用来指示缓存系统(服务器上的,或者浏览器上的)应该怎样处理缓存

请求:

Cache-Control: max-age=<seconds>
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: only-if-cached

相应:

Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>

Connection

控制当前事务完成后网络连接是否保持打开状态。

Connection: keep-alive//HTTP/1.1 的请求默认使用一个持久连接
Connection: close//表明客户端或服务器想要关闭该网络连接,这是 HTTP/1.0 请求的默认值

浏览器向服务器发送请求时发送cookie,或者服务器向浏览器附加cookie,就是将cookie附近在这里的。例如:Cookie:user=admin

由服务器端向用户代理发送 cookie

Content-Length

一个请求的请求体的内存长度,单位为字节(byte)。请求体是指在HTTP头结束后,两个CR-LF字符组之后的内容,常见的有POST提交的表单数据,这个Content-Length并不包含请求行和HTTP头的数据长度。

Content-MD5

使用base64进行了编码的请求体的MD5校验和

Content-Type

请求体中的内容的mime类型,通常只会用在POST和PUT方法的请求中。

Content-Type: text/html; charset=utf-8
Content-Type: application/x-www-form-urlencoded;charset=utf-8
Content-Type: application/json;charset=utf-8
Content-Type: multipart/form-data; boundary=something

比如post的表单:

POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575

---------------------------974767299852498929531610575
Content-Disposition: form-data; name="description"

some text
---------------------------974767299852498929531610575
Content-Disposition: form-data; name="myFile"; filename="foo.txt"
Content-Type: text/plain

(content of the uploaded file foo.txt)
---------------------------974767299852498929531610575

If-Match

这是一个条件请求,通常用在使用PUT方法对服务器资源进行更新的请求中,意思就是,询问服务器,现在正在请求的资源的tag和这个If-Match的tag相不相同,如果相同,则证明服务器上的这个资源还是旧的,现在可以被更新,如果不相同,则证明该资源被更新过,现在就不用再更新了(否则有可能覆盖掉其他人所做的更改)。

If-Modified-Since

询问服务器现在正在请求的资源在某个时间以来有没有被修改过,如果没有,服务器则返回304状态来告诉浏览器使用浏览器自己本地的缓存,如果有修改过,则返回200,并发送新的资源(当然如果资源不存在,则返回404。)

Referer

指当前请求URL是在什么地址中引用的。

X-Forwarded-For

X-Forwarded-For (XFF) 在客户端访问服务器的过程中如果需要经过 HTTP 代理或者负载均衡服务器

X-Forwarded-For: <client>, <proxy1>, <proxy2>

X-Forwarded-For: 2001:db8:85a3:8d3:1319:8a2e:370:7348

X-Forwarded-For: 203.0.113.195

X-Forwarded-For: 203.0.113.195, 70.41.3.18, 150.172.238.178
0%