【问题标题】:libcurl does not work with http redirectlibcurl 不适用于 http 重定向
【发布时间】:2014-05-09 17:26:10
【问题描述】:

我开发了一个使用 libcurl 作为 http 客户端的应用程序。

我尝试连接到一个HTTP服务器,服务器返回http重定向到另一个地址,但是libcurl收到http重定向后什么都不做。

我错过了什么?

我的代码:

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERNAME, userid);
curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, HTTP_TIMEOUT);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, HTTP_TIMEOUT);

在交通日志之后:

GET /openacs/ HTTP/1.1

Host: 192.168.1.110:8080

User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:28.0) Gecko/20100101 Firefox/28.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive



HTTP/1.1 301 Moved Permanently

Location: http://192.168.1.133:8080/openacs/acs

Date: Thu, 09 Feb 2012 15:33:15 GMT

Server: Apache/2.2.17 (Ubuntu)

Content-Length: 0

Content-Type: text/html; charset=iso-8859-1

【问题讨论】:

    标签: c curl libcurl


    【解决方案1】:

    您还需要设置 CURLOPT_FOLLOWLOCATION 标志来告诉 curl 自动跟随重定向:

    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    

    【讨论】:

    • 我试过了,效果很好。在使用旧服务器地址与服务器进行第二次连接时,libcurl 使用旧地址而不是新地址连接。我希望 libcurl 将始终在新连接中使用新地址,因为他会重定向 301 Moved Permanently
    • MOHAMED,您必须将每个连接视为独立的进程。您可以自己检测重定向和新主机名。我不会这样做明显的安全原因
    猜你喜欢
    • 2020-05-08
    • 2018-12-23
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2014-07-31
    相关资源
    最近更新 更多