我正在尝试编写一个从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
答案 0 :(得分:4)
-L设置CURLOPT_FOLLOWLOCATION
但是,正如您在响应标头中看到的那样,您可以获得200 OK,但这不是HTTP重定向,因此libcurl无需遵循。如果该URL确实具有重定向,则可以使用HTML元刷新标记完成,也可以使用javascript完成 - 这两个都不支持libcurl。