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

so 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.com

and part 2:

"\355\275k\227\034\307q \372Y\374\025\315\036\021\063\
003t\367ԣ\237\323l\340\002\003\200\204\004\200\217\001\251\a\t\367\251\256Gw\0
01\325Uͪ\352y\260\331\347X\336\265%\257,K:Z\277\326\336뵏\345ױ%\371Z\273\346
\245M\371\234\373[\b>>\355_\270\031\371\250\312\314ʪ\356!A\313\364\212C\314tgF
fFfFFDFFF>\377\354\315\310N\317\027nm\226\316\203\253\317\303\357\332\331<\b\22
3\321,M\027\207\a\a\247\247\247\255S\263\025\305\323\003}0\030\034\234Q@\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.

参考C/C++ 解压缩deflate编码

花了多半天终于调试了zlib,结果解决结果总是 Z_DATA_ERROR ,看来是输入数据有误也就是网站返回的数据有误。仔细看了头部, Transfer-Encoding: chunked,说明数据是经过分段传输过来,再次验证了一下解析chunked传输方式无误。为什么老是错误呢。。。。。。 于是自己把文件内容用deflate压缩再解压,结果OK代码正确。只是错误,说明输入数据格式有误,zlib库不认。我用的是firefox浏览器,在网站显示正常,firefox是开源的,下载源码果断看看。在mozilla-release\netwerk\streamconv\converters\ 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.