Alex Rivera | Logout

How to timeout a libcurl C++ call and/or know when a timeout has happened in the call

Asked 2011-09-04T18:54:13.350
11

I'm trying to download remote html pages with my C++ program, however with some URLs a timeout occurs, but I don't know how to handle this, so the program will just hang indefinatly.

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);
    }
}

Here is my function for downloading the html source of a page into a string variable called "pageinformation" via the "writepageinformation" function.

Edit
Report

1 Answer

3

Use the CURLOPT_TIMEOUT option?

Use the CURLOPT_PROGRESSFUNCTION callback and make the operation stop whenever you think it is enough?

Use the CURLOPT_LOWSPEED option or similar to make it depend on transfer rate.

answered 2011-09-05T11:14:57.073

Your Answer