我正在尝试编写一个从 sourceforge 下载的代码片段。这是代码,您可能会注意到我明确设置了 CURLOPT_FOLLOWLOCATION 但它似乎没有效果
int main(void) {
CURL *curl = curl_easy_init();
FILE *fp;
CURLcode res;
const char *url = "http://sourceforge.net/projects/mpc-hc/files/MPC%20HomeCinema%20-%20x64/MPC-HC_v1.7.7_x64/MPC-HC.1.7.7.x64.exe";
char outfilename[FILENAME_MAX] = "/home/snake91/example.exe";
if (curl) {
fp = fopen(outfilename,"w+");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fwrite(buffer2.c_str (), buffer2.length (), sizeof(char), fp);
fclose(fp);
}
return 0;
}
在寻找解决方案时,我发现 CURL (-L) 有一个参数可以完成这项工作,是否有与 lib 等效的东西?
ps 这是 VERBOSE 标志的输出...
* Hostname was NOT found in DNS cache
* Trying 216.34.181.60...
* Connected to sourceforge.net (216.34.181.60) port 80 (#0)
> GET /projects/mpc-hc/files/MPC%20HomeCinema%20-%20x64/MPC-HC_v1.7.7_x64/MPC-HC.1.7.7.x64.exe HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: sourceforge.net
Accept: */*
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 3047
<
* Connection #0 to host sourceforge.net left intact
最佳答案
-L 设置 CURLOPT_FOLLOWLOCATION
但是,正如您在响应 header 中看到的那样,您会返回 200 OK,这不是 HTTP 重定向,因此 libcurl 无需遵循任何内容。如果该 URL 确实具有重定向,则它要么使用 HTML 元刷新标记完成,要么使用 javascript 创建 - libcurl 均不支持。
关于c++ - libCURL 不遵循重定向(即使使用 FOLLOW_LOCATION 1L),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956470/