我試圖用我的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」的字符串變量中的函數。