2011-09-04 93 views
4

我試圖用我的C++程序下載遠程HTML頁面,但是有些URL發生超時,但我不知道如何處理這個問題,所以程序會無限地掛起來。如何超時一次libcurl C++調用和/或知道在調用中發生超時時

virtual void downloadpage(string pageaddress) { 
    CURL *curl; 
     CURLcode informationdownloaded; 
     curl = curl_easy_init(); 
     if (curl) { 
      curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"); 
      curl_easy_setopt(curl, CURLOPT_URL, pageaddress.c_str()); 
      curl_easy_setopt(curl, CURLOPT_HEADER, 0); 
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writepageinformation); 
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pageinformation); 
      informationdownloaded = curl_easy_perform(curl); 
      curl_easy_cleanup(curl); 
    } 
} 

這裏是我通過「writepageinformation」函數將頁面的html源代碼下載到名爲「pageinformation」的字符串變量中的函數。

回答

0

使用CURLOPT_TIMEOUT選項?

使用CURLOPT_PROGRESSFUNCTION回調函數,並在您認爲足夠時停止操作?

使用CURLOPT_LOWSPEED選項或類似方法使其取決於傳輸速率。

2
informationdownloaded = curl_easy_perform(curl); 

您也可以爲您的下載

curl_easy_setopt(hCurl, CURLOPT_TIMEOUT, iTimeoutSeconds); // timeout for the URL to download 

這是一個阻塞調用,直到整個文件下載指定超時。 如果你有興趣中斷阻塞調用(信號殺)安裝進度回調,像下面

curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS, 0); 
curl_easy_setopt(hCurl, CURLOPT_PROGRESSFUNCTION, progress_callback); 
curl_easy_setopt(hCurl, CURLOPT_PROGRESSDATA, this); 

static int progress_callback(void *clientp, 
          double dltotal, 
          double dlnow, 
          double ultotal, 
          double ulnow) 
{ 
    CLASS &obj = *(CLASS*)clientp; 


    if (obj.exit) 
     return 1; // if u want the signal curl to unblock and return from curl_easy_perform 

    return 0; // if u want the callback to continue 
} 
0

隨着其他建議使用CURLOPT_TIMEOUT這將使你定義一個超時,你應該檢查對於curl_easy_perform的返回值,因爲它是阻止呼叫。這裏是libcurl的doc/examples/getinfo.c的稍微修改版本,

#include <stdio.h> 
#include <stdlib.h> 
#include <curl/curl.h> 

int main(int argc, char* argv[]) 
{ 
    CURL *curl; 
    CURLcode res; 

    if (argc != 2) { 
    printf("Usage: %s <timeoutInMs>\n", argv[0]); 
    return 1; 
    } 

    curl = curl_easy_init(); 
    curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/ip"); 
    curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, atol(argv[1])); 

    res = curl_easy_perform(curl); 

    if (CURLE_OK == res) 
    printf("Success.\n"); 
    else if (CURLE_OPERATION_TIMEDOUT == res) 
    printf("Operation timed out.\n"); 

    curl_easy_cleanup(curl); 
    return 0; 
}