title: C++ LibCurl retry on error
body: <p>I want to retry a curl connection in my C++ program for 5 times. When it fails 5 times in a row, it should stop the execution of the program. However, it stops after the first error at this point. I am able to catch the error, however I don't know how to execute the previous curl connection. E.g., with jQuery I can use something like <code>$.ajax(this);</code>. For LibCurl in C++ I am looking for a similar solution.</p>
  
  <p>My current LibCurl code is shown below, note that I use multiple curl connections which all have other settings, therefore I would like a general approach which I can use for all my LibCurl errors within my <code>LibcurlError</code> function which is also included below.</p>
  
  <pre><code>curl = curl_easy_init();
  if (curl) {
      CurlResponse = "";
      host = "http://google.com";
      LibcurlHeaders = curl_slist_append(NULL, "Expect:");
      if (ProxyAddress.length() &gt; 0) {
          curl_easy_setopt(curl, CURLOPT_PROXY, ProxyAddress.c_str());
          }
      curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
      curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
      res = curl_easy_perform(curl);
      curl_slist_free_all(LibcurlHeaders);
      if (res != CURLE_OK) {
  
  
          //AT THIS POINT I WOULD LIKE TO RETRY FOR 5 TIMES WHICH I WOULD LIKE TO CATCH IN MY LibcurlError FUNCTION.
  
  
          LibcurlError(curl_easy_strerror(res), host);
          }
      curl_easy_cleanup(curl);
      }
  curl_global_cleanup();
  
  
  void LibcurlError(string error, string host) {
      //IF FAILED FOR LESS THEN 5 TIMES IN A ROW -&gt; RETRY CURL
      //ELSE I WOULD LIKE TO EXECUTE MY ORIGINAL CODE WHICH IS STATED BELOW 
  
      Message = "LibCurl Error: ";
      if (error == "Couldn't resolve host name") {
          Message.append("Couldn't connect to the server of ");
          if (host.find("google.com") != string::npos) {
              Message.append("Google");
              }
          else {
              Message.append("'" + host + "'");
              }
          }
      else {
          Message.append("'" + error + "'");
          }
      cout &lt;&lt; Message &lt;&lt; endl;
      system("pause");
      exit(0);
      }
  </code></pre>