我开发了一个使用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
答案 0 :(得分:5)
您还需要设置CURLOPT_FOLLOWLOCATION
标志,告诉curl自动关注重定向:
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);