zoukankan      html  css  js  c++  java
  • 关于http协议头

     

    转自:http://hi.baidu.com/code100line/blog/item/5c8bb08a072394779f2fb4ee.html

    Requests

    Header   Description Example
    Accept Content-Types that are acceptable Accept: text/plain
    Accept-Charset Character sets that are acceptable Accept-Charset: iso-8859-5
    Accept-Encoding Acceptable encodings Accept-Encoding: compress, gzip
    Accept-Language Acceptable languages for response Accept-Language: da
    Accept-Ranges Allows the server to indicate its acceptance of range requests for a resource Accept-Ranges: bytes
    Authorization Authentication credentials for HTTP authentication Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
    Cache-Control Used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain Cache-Control: no-cache
    Connection What type of connection the user-agent would prefer Connection: close
    Cookie an HTTP cookie previously sent by the server with Set-Cookie (below) Cookie: $Version=1; UserId=JohnDoe
    Content-Type The mime-type of the body of the request (used with POST and PUT requests) Content-Type: application/x-www-form-urlencoded
    Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
    Expect Indicates that particular server behaviors are required by the client Expect: 100-continue
    Host The domain name of the server (for virtual hosting), mandatory since HTTP/1.1 Host: en.wikipedia.org
    If-Match Only perform the action if the client supplied entity matches the same entity on the server. This is mainly for methods like PUT to only update a resource if it has not been modified since the user last updated it. If-Match: "737060cd8c284d8af7ad3082f209582d"
    If-Modified-Since Allows a 304 Not Modified to be returned if content is unchanged If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    If-None-Match Allows a 304 Not Modified to be returned if content is unchanged, see HTTP ETag If-None-Match: "737060cd8c284d8af7ad3082f209582d"
    If-Range If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity If-Range: "737060cd8c284d8af7ad3082f209582d"
    If-Unmodified-Since Only send the response if the entity has not been modified since a specific time. If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    Max-Forwards Limit the number of times the message can be forwarded through proxies or gateways. Max-Forwards: 10
    Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
    Proxy-Authorization Authorization credentials for connecting to a proxy. Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
    Range Request only part of an entity. Range: bytes=500-999
    Referer This is the address of the previous web page from which a link to the currently requested page was followed. Referer: http://en.wikipedia.org/wiki/Main_Page
    TE The transfer encodings the user is willing to accept. TE: trailers, deflate;q=0.5
    Upgrade Ask the server to upgrade to another protocol. Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
    User-Agent The user agent string of the user agent User-Agent: Mozilla/5.0 (Linux; X11)
    Via Informs the server of proxies through which the request was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
    Warn A general warning about possible problems with the entity body. Warn: 199 Miscellaneous warning


    Responses


    Header   Description Example
    Accept-Ranges What partial content range types this server supports Accept-Ranges: bytes
    Age The age the object has been in a proxy cache in seconds Age: 12
    Allow Valid actions for a specified resource. To be used for a 405 Method not allowed Allow: GET, HEAD
    Cache-Control Tells all caching mechanisms from server to client whether they may cache this object Cache-Control: no-cache
    Content-Encoding The type of encoding used on the data Content-Encoding: gzip
    Content-Language The language the content is in Content-Language: da
    Content-Length The length of the response body in 8-bit bytes Content-Length: 348
    Content-Location An alternate location for the returned data Content-Location: /index.htm
    Content-Disposition An opportunity to raise a "File Download" dialogue box for a known MIME type Content-Disposition: attachment; filename=fname.ext
    Content-MD5 An MD5 sum of the content of the response Content-MD5: 3167b9c13ad2b6d36946493fc47976c8
    Content-Range Where in a full body message this partial message belongs Content-Range: bytes 21010-47021/47022
    Content-Type The mime type of this content Content-Type: text/html; charset=utf-8
    Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
    ETag An identifier for a specific version of a resource, often a Message Digest, see ETag ETag: 737060cd8c284d8af7ad3082f209582d
    Expires Gives the date/time after which the response is considered stale Expires: Thu, 01 Dec 1994 16:00:00 GMT
    Last-Modified The last modified date for the requested object, in RFC 2822 format Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT
    Location Used in redirection, or when a new resource has been created. Location: http://www.w3.org/pub/WWW/People.html
    Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
    Proxy-Authenticate Request authentication to access the proxy. Proxy-Authenticate: Basic
    Retry-After If an entity is temporarily unavailable, this instructs the client to try again after a specified period of time. Retry-After: 120
    Server A name for the server Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
    Set-Cookie an HTTP cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
    Trailer The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding. Trailer: Max-Forwards
    Transfer-Encoding The form of encoding used to safely transfer the entity to the user. Transfer-Encoding: chunked
    Vary Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server. Vary: *
    Via Informs the client of proxies through which the response was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
    Warn A general warning about possible problems with the entity body. Warn: 199 Miscellaneous warning
    WWW-Authenticate Indicates the authentication scheme that should be used to access the requested entity. WWW-Authenticate: Basic

    ===================================================================================

    HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内 容请参考RFC2616。HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求,请求头包含请求的方法、URI、协议版本、以及包含请求修饰 符、客户信息和内容的类似于MIME的消息结构。服务器以一个状态行作为响应,相应的内容包括消息协议的版本,成功或者错误编码加上包含服务器信息、实体 元信息以及可能的实体内容。 

    通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息。这两种类型的消息由一个起 始行,一个或者多个头域,一个只是头域结束的空行和可选的消息体组成。HTTP的头域包括通用头,请求头,响应头和实体头四个部分。每个头域由一个域名, 冒号(:)和域值三部分组成。域名是大小写无关的,域值前可以添加任何数量的空格符,头域可以被扩展为多行,在每行开始处,使用至少一个空格或制表符。 

    通用头域 

    通用头域包含请求和响应消息都支持的头域,通用头域包含Cache-Control、 Connection、Date、Pragma、Transfer-Encoding、Upgrade、Via。对通用头域的扩展要求通讯双方都支持此 扩展,如果存在不支持的通用头域,一般将会作为实体头域处理。下面简单介绍几个在UPnP消息中使用的通用头域。 


    Cache-Control头域 

    Cache -Control指定请求和响应遵循的缓存机制。在请求消息或响应消息中设置 Cache-Control并不会修改另一个消息处理过程中的缓存处理过 程。请求时的缓存指令包括no-cache、no-store、max-age、 max-stale、min-fresh、only-if- cached,响应消息中的指令包括public、private、no-cache、no- store、no-transform、must- revalidate、proxy-revalidate、max-age。各个消息中的指令含义如下: 

    Public指示响应可被任何缓存区缓存。 

    Private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。 

    no-cache指示请求或响应消息不能缓存 

    no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。 

    max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。 

    min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。 

    max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。 
    Date头域 

    Date头域表示消息发送的时间,时间的描述格式由rfc822定义。例如,Date:Mon,31Dec200104:25:57GMT。Date描述的时间表示世界标准时,换算成本地时间,需要知道用户所在的时区。 


    Pragma头域 

    Pragma头域用来包含实现特定的指令,最常用的是Pragma:no-cache。在HTTP/1.1协议中,它的含义和Cache- Control:no-cache相同。 

    请求消息 

    请求消息的第一行为下面的格式: 

    MethodSPRequest -URISPHTTP-VersionCRLFMethod 表示对于Request-URI完成的方法,这个字段是大小写敏感的,包括OPTIONS、 GET、HEAD、POST、PUT、DELETE、 TRACE。方法GET和HEAD应该被所有的通用WEB服务器支持,其他所有方法的实现是可选 的。GET方法取回由Request-URI标识的信息。 HEAD方法也是取回由Request-URI标识的信息,只是可以在响应时,不返回消息体。 POST方法可以请求服务器接收包含在请求中的实体信息,可以用于提交表单,向新闻组、BBS、邮件群组和数据库发送消息。 

    SP表 示空格。Request-URI遵循URI格式,在此字段为星号(*)时,说明请求并不用于某个特定的资源地址,而是用于服务器本身。HTTP- Version表示支持的HTTP版本,例如为HTTP/1.1。CRLF表示换行回车符。请求头域允许客户端向服务器传递关于请求或者关于客户机的附 加信息。请求头域可能包含下列字段Accept、Accept-Charset、Accept- Encoding、Accept-Language、 Authorization、From、Host、If-Modified-Since、If- Match、If-None-Match、If- Range、If-Range、If-Unmodified-Since、Max-Forwards、 Proxy-Authorization、 Range、Referer、User-Agent。对请求头域的扩展要求通讯双方都支持,如果存在不支持的请求头域,一般将会作为实体头域处理。 

    典型的请求消息: 

    GET http://download.microtool.de:80/somedata.exe 
    Host: download.microtool.de 
    Accept:*/* 
    Pragma: no-cache 
    Cache-Control: no-cache 
    Referer: http://download.microtool.de/ 
    User-Agent:Mozilla/4.04[en](Win95;I;Nav) 
    Range:bytes=554554- 

    上例第一行表示HTTP客户端(可能是浏览器、下载程序)通过GET方法获得指定URL下的文件。棕色的部分表示请求头域的信息,绿色的部分表示通用头部分。 
    Host头域 

    Host头域指定请求资源的Intenet主机和端口号,必须表示请求url的原始服务器或网关的位置。HTTP/1.1请求必须包含主机头域,否则系统会以400状态码返回。 


    Referer头域 

    Referer 头域允许客户端指定请求uri的源资源地址,这可以允许服务器生成回退链表,可用来登陆、优化cache等。他也允许废除的或错误的连接由于维护的目的被 追踪。如果请求的uri没有自己的uri地址,Referer不能被发送。如果指定的是部分uri地址,则此地址应该是一个相对地址。 


    Range头域 

    Range头域可以请求实体的一个或者多个子范围。例如, 

    表示头500个字节:bytes=0-499 

    表示第二个500字节:bytes=500-999 

    表示最后500个字节:bytes=-500 

    表示500字节以后的范围:bytes=500- 

    第一个和最后一个字节:bytes=0-0,-1 

    同时指定几个范围:bytes=500-600,601-999 

    但是服务器可以忽略此请求头,如果无条件GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是以200 (OK)。 
    User-Agent头域 

    User-Agent头域的内容包含发出请求的用户信息。 

    响应消息 

    响应消息的第一行为下面的格式: 

    HTTP-VersionSPStatus-CodeSPReason-PhraseCRLF 

    HTTP -Version表示支持的HTTP版本,例如为HTTP/1.1。Status- Code是一个三个数字的结果代码。Reason-Phrase给 Status-Code提供一个简单的文本描述。Status-Code主要用于机器自动识别,Reason-Phrase主要用于帮助用户理解。 Status-Code的第一个数字定义响应的类别,后两个数字没有分类的作用。第一个数字可能取5个不同的值: 

    1xx:信息响应类,表示接收到请求并且继续处理 

    2xx:处理成功响应类,表示动作被成功接收、理解和接受 

    3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理 

    4xx:客户端错误,客户请求包含语法错误或者是不能正确执行 

    5xx:服务端错误,服务器不能正确执行一个正确的请求 

    响 应头域允许服务器传递不能放在状态行的附加信息,这些域主要描述服务器的信息和 Request-URI进一步的信息。响应头域包含Age、 Location、Proxy-Authenticate、Public、Retry- After、Server、Vary、Warning、WWW- Authenticate。对响应头域的扩展要求通讯双方都支持,如果存在不支持的响应头域,一般将会作为实体头域处理。 

    典型的响应消息: 

    HTTP/1.0200OK 

    Date:Mon,31Dec200104:25:57GMT 

    Server:Apache/1.3.14(Unix) 

    Content-type:text/html 

    Last-modified:Tue,17Apr200106:46:28GMT 

    Etag:"a030f020ac7c01:1e9f" 

    Content-length:39725426 

    Content-range:bytes554554-40279979/40279980 

    上例第一行表示HTTP服务端响应一个GET方法。棕色的部分表示响应头域的信息,绿色的部分表示通用头部分,红色的部分表示实体头域的信息。 
    Location响应头 

    Location响应头用于重定向接收者到一个新URI地址。 

    Server响应头 

    Server响应头包含处理请求的原始服务器的软件信息。此域能包含多个产品标识和注释,产品标识一般按照重要性排序。 

    实体 

    请 求消息和响应消息都可以包含实体信息,实体信息一般由实体头域和实体组成。实体头域包含关于实体的原信息,实体头包括Allow、Content- Base、Content-Encoding、Content-Language、 Content-Length、Content- Location、Content-MD5、Content-Range、Content-Type、 Etag、Expires、Last- Modified、extension-header。extension-header允许客户端定义新的实体头,但是这些域可能无法未接受方识别。实 体可以是一个经过编码的字节流,它的编码方式由Content-Encoding或Content-Type定义,它的长度由Content- Length或Content-Range定义。 

    Content-Type实体头 

    Content-Type实体头用于向接收方指示实体的介质类型,指定HEAD方法送到接收方的实体介质类型,或GET方法发送的请求介质类型 Content-Range实体头 

    Content-Range实体头用于指定整个实体中的一部分的插入位置,他也指示了整个实体的长度。在服务器向客户返回一个部分响应,它必须描述响应覆盖的范围和整个实体长度。一般格式: 

    Content-Range:bytes-unitSPfirst-byte-pos-last-byte-pos/entity-legth 

    例 如,传送头500个字节次字段的形式:Content-Range:bytes0- 499/1234如果一个http消息包含此节(例如,对范围请求的 响应或对一系列范围的重叠请求),Content-Range表示传送的范围, Content-Length表示实际传送的字节数。 

    Last-modified实体头 

    Last-modified实体头指定服务器上保存内容的最后修订时间。

    应答头 说明
    Allow 服务器支持哪些请求方法(如GET、POST等)。
    Content-Encoding 文档的编码(Encode)方法。只有在解码之后才可以得到Content-Type头指定的内容类型。利用gzip压缩文档能够显著地减少 HTML文档的下载时间。Java的GZIPOutputStream可以很方便地进行gzip压缩,但只有Unix上的Netscape和 Windows上的IE 4、IE 5才支持它。因此,Servlet应该通过查看Accept-Encoding头(即request.getHeader("Accept- Encoding"))检查浏览器是否支持gzip,为支持gzip的浏览器返回经gzip压缩的HTML页面,为其他浏览器返回普通页面。
    Content-Length 表示内容长度。只有当浏览器使用持久HTTP连接时才需要这个数据。如果你想要利用持久连接的优势,可以把输出文档写入 ByteArrayOutputStram,完成后查看其大小,然后把该值放入Content-Length头,最后通过 byteArrayStream.writeTo(response.getOutputStream()发送内容。
    Content-Type 表示后面的文档属于什么MIME类型。Servlet默认为text/plain,但通常需要显式地指定为text/html。由于经常要设置Content-Type,因此HttpServletResponse提供了一个专用的方法setContentTyep。
    Date 当前的GMT时间。你可以用setDateHeader来设置这个头以避免转换时间格式的麻烦。
    Expires 应该在什么时候认为文档已经过期,从而不再缓存它?
    Last-Modified 文档的最后改动时间。客户可以通过If-Modified-Since请求头提供一个日期,该请求将被视为一个条件GET,只有改动时间迟于指定 时间的文档才会返回,否则返回一个304(Not Modified)状态。Last-Modified也可用setDateHeader方法来设置。
    Location 表示客户应当到哪里去提取文档。Location通常不是直接设置的,而是通过HttpServletResponse的sendRedirect方法,该方法同时设置状态代码为302。
    Refresh 表示浏览器应该在多少时间之后刷新文档,以秒计。除了刷新当前文档之外,你还可以通过setHeader("Refresh", "5; URL=http://host/path")让浏览器读取指定的页面。 
    注 意这种功能通常是通过设置HTML页面HEAD区的<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://host/path">实现,这是因为,自动刷新或重定向对于那些不能使用CGI或Servlet的 HTML编写者十分重要。但是,对于Servlet来说,直接设置Refresh头更加方便。 

    注意Refresh的意义是“N秒之后 刷新本页面或访问指定页面”,而不是“每隔N秒刷新本页面或访问指定页面”。因此,连续刷新要求每次都发送一个Refresh头,而发送204状态代码则 可以阻止浏览器继续刷新,不管是使用Refresh头还是<META HTTP-EQUIV="Refresh" ...>。 

    注意Refresh头不属于HTTP 1.1正式规范的一部分,而是一个扩展,但Netscape和IE都支持它。
    Server 服务器名字。Servlet一般不设置这个值,而是由Web服务器自己设置。
    Set-Cookie 设置和页面关联的Cookie。Servlet不应使用response.setHeader("Set-Cookie", ...),而是应使用HttpServletResponse提供的专用方法addCookie。参见下文有关Cookie设置的讨论。
    WWW-Authenticate 客户应该在Authorization头中提供什么类型的授权信息?在包含401(Unauthorized)状态行的应答中这个头是必需的。例 如,response.setHeader("WWW-Authenticate", "BASIC realm=\"executives\"")。 
    注意Servlet一般不进行这方面的处理,而是让Web服务器的专门机制来控制受密码保护页面的访问(例如.htaccess)。
     
  • 相关阅读:
    react.js+axios跨域
    O2O项目之一 环境搭配
    跟scss相关的两个包
    [nodemon] app crashed
    解决node.js链接数据库时出现的报错 --- client does not support authentication
    在Xshell 运行angular 项目时,找不到node-sass模块,安装node-sass模块时,又出现权限问题
    ajax请求数据时,get和post的区别
    web前端如何性能优化提高加载速度
    js数组去重
    前端跨域
  • 原文地址:https://www.cnblogs.com/dingdingmao/p/3146526.html
Copyright © 2011-2022 走看看