-

本章将介绍几个重要的HTTP协议参数及其语法在通信中的使用方式。例如,日期格式,URL格式等。这将帮助您在编写HTTP客户端或服务器程序时创建请求和响应消息。在学习HTTP请求和响应的消息结构时,将在后续章节中看到这些参数的完整用法。

HTTP版本

HTTP uses a <major>.<minor> numbering scheme to indicate versions of the protocol. The version of an HTTP message is indicated by an HTTP-Version field in the first line. Here is the general syntax of specifying HTTP version number:

HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT

Example

HTTP/1.0

or

HTTP/1.1

Uniform Resource Identifiers

Uniform Resource Identifiers (URI) are simply formatted, case-insensitive string containing name, location, etc. to identify a resource, for example, a website, a web service, etc. A general syntax of URI used for HTTP is as follows:

URI = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

Here if the port is empty or not given, port 80 is assumed for HTTP and an empty abs_path is equivalent to an abs_path of "/". The characters other than those in the reserved and unsafe sets are equivalent to their ""%" HEX HEX" encoding.

Example

The following three URIs are equivalent:

http://abc.com:80/~smith/home.html
http://ABC.com/%7Esmith/home.html
http://ABC.com:/%7esmith/home.html

Date/Time Formats

All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. HTTP applications are allowed to use any of the following three representations of date/time stamps:

Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov  6 08:49:37 1994       ; ANSI C"s asctime() format

Character Sets

We use character sets to specify the character sets that the client prefers. Multiple character sets can be listed separated by commas. If a value is not specified, the default is the US-ASCII.

Example

以下是有效的字符集:

US-ASCII

or

ISO-8859-1

or 

ISO-8859-7

内容编码

内容编码值表示在通过网络通过之前已经使用编码算法对内容进行编码。内容编码主要用于允许文档被压缩或以其他方式有效地转换而不会丢失标识。

所有内容编码值都不区分大小写。HTTP / 1.1使用接受编码和内容编码头字段中的内容编码值,我们将在后续章节中看到。

以下是有效的编码方案:

Accept-encoding: gzip

or

Accept-encoding: compress

or 

Accept-encoding: deflate

媒体类型

HTTP在Content-TypeAccept头域中使用Internet媒体类型,以提供开放和可扩展的数据输入和类型协商。所有媒体类型的值都被注册到互联网号码分配机构(IANA)。指定媒体类型的一般语法如下:

media-type     = type "/" subtype *( ";" parameter )

类型,子类型和参数属性名称不区分大小写。

Accept: image/gif

语言标签

HTTP使用Accept-LanguageContent-Language字段中的语言标签语言标签由一个或多个部分组成:主要语言标签和可能为空的一系列子标签:

language-tag  = primary-tag *( "-" subtag )

标签中不允许使用空格,所有标签都不区分大小写。

示例代码包括:

en, en-US, en-cockney, i-cherokee, x-pig-latin

其中任何两个字母的主要标签是ISO-639语言缩写,任何两个字母的初始子标签都是ISO-3166国家代码。