我希望在我的C++程序中重试curl连接5次。当连续失败5次时,应该停止程序的执行。然而,在这一点上它只会在第一个错误后停止。我能够捕获错误,但是我不知道如何执行以前的curl连接。例如,使用jQuery,我可以使用类似
我的当前LibCurl代码如下所示,请注意我使用多个curl连接,它们都有其他设置,因此我希望有一个通用的方法,可以在我的
$.ajax(this);
的东西。对于C++中的LibCurl,我正在寻找类似的解决方案。我的当前LibCurl代码如下所示,请注意我使用多个curl连接,它们都有其他设置,因此我希望有一个通用的方法,可以在我的
LibcurlError
函数中用于所有LibCurl错误。curl = curl_easy_init();
if (curl) {
CurlResponse = "";
host = "http://google.com";
LibcurlHeaders = curl_slist_append(NULL, "Expect:");
if (ProxyAddress.length() > 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 -> 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 << Message << endl;
system("pause");
exit(0);
}
curl_easy_perform
,您可以根据需要调用多少次。具体问题是什么? - Igor TandetnikLibcurlHeaders
的活性,只要您打算使用它。 - Igor Tandetnik