Http 简明教程

HTTP - Parameters

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

This chapter is going to list down few of the important HTTP Protocol Parameters and their syntax the way they are used in the communication. For example, format for date, format of URL, etc. This will help you in constructing your request and response messages while writing HTTP client or server programs. You will see the complete usage of these parameters in subsequent chapters while learning the message structure for HTTP requests and responses.

HTTP Version

HTTP 使用编号方案来表示协议的版本。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

统一资源标识符 (URI) 只是格式化、不区分大小写的字符串,其中包含用于标识资源(例如网站、网络服务等)的名称、位置等。用于 HTTP 的 URI 的一般语法如下:

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 ]]

如果 {} 中的内容为空或未给出,则假定 HTTP 端口为 80,而空 {} 等效于 “/”。{} 和 {} 集之外的字符等效于它们的 ""%" HEX HEX" 编码。

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

以下三个 URI 等效:

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

所有HTTP日期/时间戳都必须以格林尼治标准时间(GMT)表示,无一例外。HTTP应用程序可以使用以下三种日期/时间戳表示形式中的任何一种:

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

我们使用字符集来指定客户端首选的字符集。可以用逗号分隔列出多个字符集。如果未指定值,则默认为 US-ASCII。

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

以下为有效字符集:

Following are the valid character sets:

US-ASCII

or

ISO-8859-1

or

ISO-8859-7

Content Encodings

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

A content encoding value indicates that an encoding algorithm has been used to encode the content before passing it over the network. Content coding are primarily used to allow a document to be compressed or otherwise usefully transformed without losing the identity.

所有内容编码值均不区分大小写。HTTP/1.1 在后续章节中将看到的 Accept-Encoding 和 Content-Encoding 头字段中使用内容编码值。

All content-coding values are case-insensitive. HTTP/1.1 uses content-coding values in the Accept-Encoding and Content-Encoding header fields which we will see in the subsequent chapters.

Example

以下是有效的编码方案:

Following are the valid encoding schemes:

Accept-encoding: gzip

or

Accept-encoding: compress

or

Accept-encoding: deflate

Media Types

HTTP 在 Content-TypeAccept 头字段中使用网络媒体类型,目的是提供开放式且可扩展的数据类型和类型协商。所有的媒体类型值都已在互联网号码分配局 (IANA) 中注册。指定媒体类型的通用语法如下:

HTTP uses Internet Media Types in the Content-Type and Accept header fields in order to provide open and extensible data typing and type negotiation. All the Media-type values are registered with the Internet Assigned Number Authority (IANA). The general syntax to specify media type is as follows:

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

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

The type, subtype, and parameter attribute names are case—​insensitive.

Example

Accept: image/gif

Language Tags

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

HTTP uses language tags within the Accept-Language and Content-Language fields. A language tag is composed of one or more parts: a primary language tag and a possibly empty series of subtags:

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

标签内不允许有空格且所有标签均不区分大小写。

White spaces are not allowed within the tag and all tags are case- insensitive.

Example

示例标签包括:

Example tags include:

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

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

where any two-letter primary-tag is an ISO-639 language abbreviation and any two-letter initial subtag is an ISO-3166 country code.