libcurl不适用于http重定向

libcurl不适用于http重定向,c,curl,libcurl,C,Curl,Libcurl,我开发了一个将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_eas

我开发了一个将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
您还需要设置标志,告知curl自动遵循重定向:

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

我试过了,效果很好。在使用旧服务器地址与服务器的第二次连接中,libcurl使用旧地址而不是新地址进行连接。我希望libcurl总是在新连接中使用新地址,因为他将重定向
301永久移动
MOHAMED,您必须将每个连接看作独立的进程。您可以自己检测重定向和新主机名。出于明显的安全原因,我不会这么做