今天我调用
Win32 Internet 函数发送 HTTP封包, 来获取路由器的 chklst.txt 页面. 其中涉及到对用户名和密码加密的问题, 对用户名加密后要将
base64 码 YWRtaW46YWRtaW4= ( “admin:admin” 加密的结果, 用户名和密码都是 admin, 中间用 : 隔开) 以
Authorization: Basic YWRtaW46YWRtaW4= 的形式加在
Request Headers 里面.
我通过网页获取, 抓到的封包如下所示 :
GET /chklst.txt
HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Host: 192.168.10.1
Connection: Keep-Alive
Authorization: Basic YWRtaW46YWRtaW4=
HTTP/1.1 200 OK
Date: Sat, 01 Jan 2000 00:02:02 GMT
Server: Boa/0.94.14rc21
Accept-Ranges: bytes
Connection: close
Content-Length: 169
Last-Modified: Sat, 01 Jan 2000 00:00:33 GMT
Content-Type: text/plain
…(数据部分)
因为Accept, Accept-Language, UA-CPU, Accept-Encoding 及 Connection 等信息是可要可不要的, 于是我模仿它发送封包. 本来我想这应该没问题的, 我通过抓包发现 :
GET /chklst.txt
Http/1.1
Authorization: Basic YWRtaW46YWRtaW4=
User-Agent: WinInet Example1
Host: 192.168.10.1
Cache-Control: no-cache
HTTP/1.0 400 Bad Request
Date: Sat, 01 Jan 2000 00:04:44 GMT
Server: Boa/0.94.14rc21
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=ISO-8859-1
…(数据部分)
封包返回
400 Bad Request , 这表明我请求没有成功, 后来我找了很久, 包括把这些Request Headers 加得和网页获取页面的一模一样, 但是得到的结果都是
400 Bad Request .
后来我突然发现网页发送封包和我自己发送的封包的响应信息有所不同, 网页的是
HTTP/1.1 200 OK, 我自己的是
HTTP/1.0 400 Bad Request, 竟然 HTTP 协议版本不同, 我马上查询我的请求头关于 HTTP 协议版本信息, 发现是 GET /chklst.txt
Http/1.1 , 而网页发送的是 GET /chklst.txt
HTTP/1.1 , 只是大小写不同, 却导致两种完全不同的结果.
我回到程序找到 HttpOpenRequest 函数 :
hRequest = HttpOpenRequest(
hConnect // Handle to an HTTP session returned by InternetConnect
, szVerb // the HTTP verb to use in the request
, szObjectName // the name of the target object of the specified HTTP verb
, "HTTP/1.1" // the HTTP version "HTTP/1.1"
, NULL // the URL of the document from which the URL in the request was obtained
, NULL // array of strings that indicates media types accepted by the client
, dwFlag // Internet options
, 1 // the application-defined value that associates this operation with any application data
);
将参数
"Http/1.1" 改成
"HTTP/1.1" 或者
NULL 就可以了.
我想是因为我发送的封包中协议版本信息
Http/1.1 , 路由器不能识别, 而把它当成
HTTP/1.0 处理了. 但是有些路由器或者服务器, 可以把它当成
HTTP/1.1 处理.
我想一定有什么
更合理的解释, 所以希望大家能够毫不吝啬的告诉我, 或者告诉我 HTTP/1.0 和 HTTP/1.1 的
具体区别也可以 .