http(1.1版本)协议的简要内容如下:
client request
GET /develop.html HTTP/1.1<CRTL>
Accept: */*<CRTL>
Referer: http://localhost/index.html<CRTL>
Accept-Language: zh-cn<CRTL>
Accept-Encoding: gzip, deflate<CRTL>
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)<CRTL>
Host: localhost<CRTL>
Connection: Keep-Alive<CRTL>
server response
HTTP/1.1 400 Bad Request<CRTL>
Server: Apache-Coyote/1.1<CRTL>
Transfer-Encoding: chunked<CRTL>
Date: Mon, 19 Jun 2006 01:54:35 GMT<CRTL>
Connection: close<CRTL>
HTTP/1.1 200 OK<CRTL>
Server: Apache-Coyote/1.1<CRTL>
ETag: W/"6381-1144994988000"<CRTL>
Last-Modified: Fri, 14 Apr 2006 06:09:48 GMT<CRTL>
Content-Type: text/html[实际类型,火狐不支持text/plain]<CRTL>
Content-Length: 6381[实际长度]<CRTL>
Date: Mon, 19 Jun 2006 05:29:09 GMT<CRTL>
Connection: keep-alive<CRTL>
<CRTL>
[DATA]
简单说明如下:
client request 表示客户端发送的请求,并且请求必须要下面指定的格式发送。
<CRTL> 回车换行
GET /develop.html HTTP/1.1<CRTL> GET是请求方法,develop.html表示请求文件的名称
Accept: */*<CRTL> 接受所有类型的数据
Referer: http://localhost/index.html<CRTL> 所请求文件的地址
Accept-Language: zh-cn<CRTL> 接受的语言为中文
Accept-Encoding: gzip, deflate<CRTL> 所接受的压缩方式
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)<CRTL> 用户代理
Host: localhost<CRTL> 主机地址(自己机器地址)
Connection: Keep-Alive<CRTL> 连接方式
server response 服务器响应方式有两种
HTTP/1.1 400 Bad Request<CRTL> 说明出现400等错误
HTTP/1.1 200 OK<CRTL> 说明服务器成功响应
Content-Length: 6381[实际长度]<CRTL> 从这段字符串中能得到文件的长度
Connection: keep-alive<CRTL>
<CRTL>
[DATA]
这三段数据:第一行表示连接方式,然后两个回车换行之后是服务器发给客户端的数据。
http协议的简单应用(下载功能的简单实现):
(1) 首先客户端给服务器发送一个请求
(2) 服务端响应客户段的请求
(3) 如果出现400等说明出现错误
(4) 服务器成功响应则可以下载某资源
(5) 首先用strstr找到服务器发送的信息中出现两个回车换行的位置,后面的[DATA]则是我们要下载的数据。