How to disable libcurl C ++ call and / or find out when a call timed out

I am trying to load remote html pages using my C ++ program, however a timeout occurs with some URLs, but I don’t know how to handle it, so the program will just freeze.

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.ABC 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 to load the html source of the page into a string variable called "pageinformation" via the "writepageinformation" function.

+4
source share
3 answers
 informationdownloaded = curl_easy_perform(curl); 

You can also specify a timeout for loading

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

This is a blocked call until the entire file is downloaded. If you are interested in interrupting a blocked call (for a kill signal), set the progress callback, for example below

 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 } 
+2
source

Use parameter CURLOPT_TIMEOUT?

Use the CURLOPT_PROGRESSFUNCTION callback and stop if you think this is enough?

Use the CURLOPT_LOWSPEED or similar parameter so that it depends on the transmission speed.

0
source

Along with other suggestions for using CURLOPT_TIMEOUT that will allow you to determine the timeout, you should simply check the return value of curl_easy_perform , as this is a blocking call. Here is a slightly modified version of doc / examples / getinfo.c libcurl,

 #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; } 
0
source

All Articles