【发布时间】:2011-11-10 05:09:53
【问题描述】:
我正在尝试使用我的 C++ 程序下载远程 html 页面,但是某些 URL 会发生超时,但我不知道如何处理,所以程序将无限期挂起。
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”的字符串变量的函数。
【问题讨论】: