c++ - libCURL 不遵循重定向(即使使用 FOLLOW_LOCATION 1L)

标签 c++ network-programming libcurl

我正在尝试编写一个从 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/

相关文章:

c++ - 使用 gcc 将字符串文字的编码设置为 latin1

java - 在套接字上发送和接收文件

c++ - 客户端-服务器程序,使用 TCP 从客户端向服务器发送消息时出现问题,反之亦然

c++ - 通过 TCP 套接字接收可变大小的数据

cookies - 将 libCurl 与 Bluecoat cookie 代理一起使用

c++ - 在 xcode 中静态链接 libcurl 时出现问题

c++ - int 测试(无符号整数 i = -1);为什么编译器不生成警告?

c++ - 使用 yaml-cpp 转换为模板类

c++ - 获取成员函数内部成员函数的地址

c++ - CMake 找不到 libcurl-config.cmake