问题
I got data from web server:
data := '
HTTP/1.1 200 OK
Content-Encoding: gzip
Vary: Accept-Encoding
Content-type: text/html
Transfer-Encoding: chunked
Server: Apache
3d5
????????????????????????????????????
????????????????????????????????????
????????????????????????????????????
';
The size of data is: 3d5 (in hex) all is stored to TIdBytes variable "data".
How to decode gziped data, change something in it, and encode back and edit the length 3d5 to new.
回答1:
The Transfer-Encoding
response header is set to chunked
. That means the server sends the body data in chunks, where each chunk indicates its own size, where a 0-length chunk indicates the end of the data. The 3d5
refers to the size of the first chunk. That would be the full size of the HTML only if there is just 1 chunk of data in the response.
TIdHTTP
internally handles chunked data for you. If the de-chunked data has been gzip'ped, TIdHTTP
can decompress it for you if you assign a TIdZLibCompressorBase
-derived component, such as TIdCompressorZLib
, to the TIdHTTP.Compressor
property beforehand.
回答2:
You can decompress a http gzipped body with the GZDecompressStr()
function of ZlibExGz unit. Just pass the data, exactly how you get it from the http response message, as a parameter and it will return the decompressed data.
uses ZlibExGz;
var s:string;
begin
// read the gzipped data in "s"
s:=GZDecompressStr(s);
// now "s" contains uncompressed data
end;
来源:https://stackoverflow.com/questions/8597009/how-to-decode-gzip-encoded-html