问题描述
- the response string from baidu
-
** i send the request string to www.baidu.com:**GET / HTTP/1.1
Host: www.baidu.com
User-Agent: Mozilla/5.0 (X11; Linux x86_**64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: BAIDUID=9C08E9A7478B349B70427EE7A5BBC2D5:FG=1; BAIDUPSID=9C08E9A7478B349B70427EE7A5BBC2D5; BDUSS=QxT2h3RW9qMU5WVm9MWU9tNm5hfmZZU2VPMFhXVUhhd2Z5bU5mZmxybWlCbjlVQVFBQUFBJCQAAAAAAAAAAAEAAABZ0NELbGlmYW5neHZhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKJ5V1SieVdUNG; BD_UPN=133352
Connection: keep-aliveso i can get some apply string part 1:
HTTP/1.1 200 OK
Date: Sat, 10 Jan 2015 12:30:11 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: Keep-Alive
Cache-Control: private
Expires: Sat, 10 Jan 2015 12:30:11 GMT
Content-Encoding: gzip
Server: BWS/1.1
BDPAGETYPE: 2
BDQID: 0x8c10a2fd00002a56
BDUSERID: 198299737
Set-Cookie: BDSVRTM=203; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=1466_10674_10873_10502_10500_11059_11067_10923_10700_10990_10618; path=/; domain=.baidu.comand part 2:
"355275k22734307q 372Y37425315362163
003t367?237323l34002032002040420021701251at367251256Gw
01325U?352y260331347X336265%257,K:Z277326336?345?%371Z273346
245M371234373[b>>355_27031371250312314?356!A313364212C314tgF
fFfFFDFFF>377354315310N31727nm226316203253317303357332331<b22
3321,M27207aa247247247255S2632530532303}03034234Q@327r..."from part 1,i see this "Content-Encoding: gzip",
so i try to decompress the part 2 string using zlib.h.
but the function "ret = inflate(&strm, Z_NO_FLUSH);"
return -3 : Z_DATA_ERROR means that invalid or incomplete deflate data.
if you can help me ,please.
解决方案
花了多半天终于调试了zlib,结果解决结果总是 Z_DATA_ERROR ,看来是输入数据有误也就是网站返回的数据有误。仔细看了头部, Transfer-Encoding: chunked,说明数据是经过分段传输过来,再次验证了一下解析chunked传输方式无误。为什么老是错误呢。。。。。。 于是自己把文件内容用deflate压缩再解压,结果OK代码正确。只是错误,说明输入数据格式有误,zlib库不认。我用的是firefox浏览器,在网站显示正常,firefox是开源的,下载源码果断看看。在mozilla-releasenetwerkstreamconvconverters nsHTTPCompressConv.cpp 仔细看了好一阵子果然发现了猫腻
你可以参考一下nsHTTPCompressConv.cpp
解决方案二:
gzip编码,deflate解码
解决方案三:
The response is end in chunk (as indicated in header Transfer-Encoding: chunked). You may need to concat the response before decompress.