2014-05-09 59 views
2

我開發了一個使用libcurl作爲http客戶端的應用程序。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 

回答

4

您還需要設置CURLOPT_FOLLOWLOCATION標誌告訴捲曲自動執行重定向:

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 
+0

我嘗試過,它的工作。在第二次連接到具有舊服務器地址的服務器時,libcurl與舊地址連接,而不是與新地址連接。我期望libcurl將始終在新連接中使用新地址,因爲他獲得了「301永久移動」重定向 – MOHAMED