第2节 HTTP协议
超文本传输协议(HTTP)是万维网(World wide web)的通信协议,可以通过Internet传输网页(超文本文件)。HTTP连接由两部分组成:HTTP客户端(Web浏览器)和HTTP服务器。可以用HTTP的客户端接收和查看网页,用HTTP的服务器存储、管理和传输网页。

图 1 HTTP基本模块图
HTTP是由技术规范RFC2616(HTTP的 1.1 版本)和RFE1945(HTTP的1.0 版本) 定义 的 。 RFC 规范是由IETF 发 布 的 。 详情 请 参见HTUhttp://www.rfc-editor.orgUTH。
HTTP是一种请求响应协议。客户端向服务器发出一个网页请求,服务器以该网页的内容(HTML-超文本标记语言)作为响应。HTTP可以发送包括二进制数据的任何类型的数据。客户端使用GET方法(HTTP是一种ASCII协议)请求一个文件。服务器以一个HTTP首部作为响应,该首部后面就是文件的内容。客户端也可以通过 POST 方法发送一个文件请求。HTTP 的版本是以 ASCII 的类型被嵌入到该请求中。这样可以让服务器知道客户端的一些局限性。

图 2 HTTP请求和响应基本模块图
2.1 HTTP请求实例
客户端(web浏览器)发送到HTTP服务器的请求如下:
GET /filename.htm HTTP/1.1
# Asks the server to respond with the contents of filename.htm
# Tells the server that it supports the HTTP1.1 standard
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword
# Tells the server that it supports: gif, x-xbitmaps, jpeg, and pjpeg images,
# and msword documents
Accept-language: en-us
# Tells the server that the language is English Accept-Encoding: gzip, deflate
# The gzip and deflate decompression algorithms are available
User-Agent: Mozzilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)
# Tells the server that the browser is running IE6.0 on a Windows computer
Connection: Keep-Alive
# Tells the server not to close the connection after the file is sent
2.2 HTTP方法
GET方法是RFC2616所支持的方法之一。表1中列出了其他的方法。
2.3 HTTP响应实例
服务器对GET方法的响应,首部如下:
HTTP/1.1 200 OK
# Tells the client/browser that HTTP1.1 is supported, and the 200 status code tells
# the client that the file was found
Server: Microsoft-IIS/6.0
# Informs the client of the web server type and version
Cache-Control: no-cache
# Tells the client to disable cache
Content-Type: text/html
# Tells the client the type of data that follows
Content-Encoding: gzip
# Tells the client that the following data is encrypted using gzip
Content-Length: 9062
# Tells the client how many bytes are to follow
接下来就是文件内容,本例使用gzip编码。
2.4 持久连接
持久连接或KEEP ALIVE 是一种协议特征,通过减少TCP/IP overhead用来提高性能。一个普通的非持久连接的HTTP事务包括:
● TCP/IP 连接
● GET 方法 AN3518 4
● 文件传输
● TCP/IP 断开
客户端需要的每个文件都要执行该过程(通常一个网页需要多次)。TCP/IP 连接需要花费很多时间。对应持久连接,TCP/IP 连接只发生在第一个文件传输之前,文件传输完毕不会关闭该连接。反之,服务器进入等待下一个方法的状态。

图 3 非持久连接的HTTP事务

图 4 持久连接的HTTP事务

图 1 HTTP基本模块图
HTTP是由技术规范RFC2616(HTTP的 1.1 版本)和RFE1945(HTTP的1.0 版本) 定义 的 。 RFC 规范是由IETF 发 布 的 。 详情 请 参见HTUhttp://www.rfc-editor.orgUTH。
HTTP是一种请求响应协议。客户端向服务器发出一个网页请求,服务器以该网页的内容(HTML-超文本标记语言)作为响应。HTTP可以发送包括二进制数据的任何类型的数据。客户端使用GET方法(HTTP是一种ASCII协议)请求一个文件。服务器以一个HTTP首部作为响应,该首部后面就是文件的内容。客户端也可以通过 POST 方法发送一个文件请求。HTTP 的版本是以 ASCII 的类型被嵌入到该请求中。这样可以让服务器知道客户端的一些局限性。

图 2 HTTP请求和响应基本模块图
2.1 HTTP请求实例
客户端(web浏览器)发送到HTTP服务器的请求如下:
GET /filename.htm HTTP/1.1
# Asks the server to respond with the contents of filename.htm
# Tells the server that it supports the HTTP1.1 standard
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword
# Tells the server that it supports: gif, x-xbitmaps, jpeg, and pjpeg images,
# and msword documents
Accept-language: en-us
# Tells the server that the language is English Accept-Encoding: gzip, deflate
# The gzip and deflate decompression algorithms are available
User-Agent: Mozzilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)
# Tells the server that the browser is running IE6.0 on a Windows computer
Connection: Keep-Alive
# Tells the server not to close the connection after the file is sent
2.2 HTTP方法
GET方法是RFC2616所支持的方法之一。表1中列出了其他的方法。
表 1 RFC2616 方法


2.3 HTTP响应实例
服务器对GET方法的响应,首部如下:
HTTP/1.1 200 OK
# Tells the client/browser that HTTP1.1 is supported, and the 200 status code tells
# the client that the file was found
Server: Microsoft-IIS/6.0
# Informs the client of the web server type and version
Cache-Control: no-cache
# Tells the client to disable cache
Content-Type: text/html
# Tells the client the type of data that follows
Content-Encoding: gzip
# Tells the client that the following data is encrypted using gzip
Content-Length: 9062
# Tells the client how many bytes are to follow
接下来就是文件内容,本例使用gzip编码。
2.4 持久连接
持久连接或KEEP ALIVE 是一种协议特征,通过减少TCP/IP overhead用来提高性能。一个普通的非持久连接的HTTP事务包括:
● TCP/IP 连接
● GET 方法 AN3518 4
● 文件传输
● TCP/IP 断开
客户端需要的每个文件都要执行该过程(通常一个网页需要多次)。TCP/IP 连接需要花费很多时间。对应持久连接,TCP/IP 连接只发生在第一个文件传输之前,文件传输完毕不会关闭该连接。反之,服务器进入等待下一个方法的状态。

图 3 非持久连接的HTTP事务

图 4 持久连接的HTTP事务


