I added curl_easy_setopt(client, CURLOPT_ENCODING, "gzip");
到我的代码。
我预计curl 会导致服务器发送压缩数据并解压缩它。
实际上我在 HTTP 标头中看到数据被压缩(变化:Accept-Encoding
内容编码:gzip),但curl不会为我解压缩它。
我应该为此使用额外的命令吗?
请注意,该选项已重命名为CURLOPT_ACCEPT_ENCODING http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTACCEPTENCODING.
正如文档所述:
设置 HTTP 请求中发送的 Accept-Encoding: 标头的内容,并在收到 Content-Encoding: 标头时启用响应解码。
所以它确实解码(即解压缩)响应。支持三种编码:"identity"
(什么也没做),"zlib"
and "gzip"
。或者,您可以传递一个空字符串来创建一个Accept-Encoding:
包含所有支持的编码的标头。
终于,httpbin http://httpbin.org/测试它很方便,因为它包含一个返回 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
...
JSON 响应(因此解压缩)被写入标准输出。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)