如何解码gzip编码的html?
我从网络服务器获取数据:
data := '
HTTP/1.1 200 OK
Content-Encoding: gzip
Vary: Accept-Encoding
Content-type: text/html
Transfer-Encoding: chunked
Server: Apache
3d5
????????????????????????????????????
????????????????????????????????????
????????????????????????????????????
';
数据大小为:3d5(十六进制) 所有内容都存储到 TIdBytes 变量“data”中。
如何解码 gzip 压缩数据、更改其中的某些内容、编码回来并将长度 3d5 编辑为新的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Transfer-Encoding
响应标头设置为chunked
。这意味着服务器以块的形式发送正文数据,其中每个块指示其自己的大小,其中0长度的块指示数据的结尾。3d5
指的是第一个块的大小。仅当响应中只有 1 块数据时,这才是 HTML 的完整大小。TIdHTTP
在内部为您处理分块数据。如果分块数据已被 gzip 压缩,如果您分配一个TIdZLibCompressorBase
派生组件(例如TIdCompressorZLibTIdHTTP
可以为您解压缩它>,预先添加到TIdHTTP.Compressor
属性。The
Transfer-Encoding
response header is set tochunked
. 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. The3d5
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 aTIdZLibCompressorBase
-derived component, such asTIdCompressorZLib
, to theTIdHTTP.Compressor
property beforehand.您可以使用 ZlibExGz 的
GZDecompressStr()
函数解压缩 http gzip 压缩正文 单位。只需将数据(确切地说是如何从 http 响应消息中获取数据)作为参数传递,它将返回解压缩的数据。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.