我添加curl_easy_setopt(client, CURLOPT_ENCODING, "gzip");
到我的代码中.
我期望curl导致服务器发送压缩数据并解压缩.
实际上我在HTTP标题中看到数据被压缩(Vary:Accept-Encoding Content-Encoding:gzip),但curl并没有为我解压缩它.
我应该使用额外的命令吗?
请注意,此选项已重命名为CURLOPT_ACCEPT_ENCODING
.
如文件所述:
设置HTTP请求中发送的Accept-Encoding:标头的内容,并在收到Content-Encoding:标头时启用响应解码.
所以它确实解码(即解压缩)响应.支持三种编码:( "identity"
什么都不做),"zlib"
和"gzip"
.或者,您可以传递一个空字符串,该字符串会创建Accept-Encoding:
包含所有支持的编码的标头.
最后,httpbin很方便测试它,因为它包含一个返回gzip内容的专用端点.这是一个例子:
#include <curl/curl.h> int main(void) { CURLcode rc; CURL *curl; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/gzip"); curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip"); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); rc = curl_easy_perform(curl); curl_easy_cleanup(curl); return (int) rc; }
它发送:
GET /gzip HTTP/1.1 Host: httpbin.org Accept: */* Accept-Encoding: gzip
得到回应:
HTTP/1.1 200 OK Access-Control-Allow-Origin: * Content-Encoding: gzip Content-Type: application/json ...
并且在stdout上写入JSON响应(因此解压缩).