C libcurl错误重试
C++ LibCurl retry on error
我想在我的C 程序中重试卷曲连接5次。当它连续发生5次失败时,它应该停止执行程序。但是,此时第一个错误之后停止。我能够捕获错误,但是我不知道如何执行先前的卷发连接。例如,使用jQuery,我可以使用$.ajax(this);
之类的东西。对于c 中的libcurl,我正在寻找类似的解决方案。
我当前的libcurl代码如下所示,请注意,我使用的多个卷曲连接都具有其他设置,因此我想要一种通用方法,我可以在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
来完成。
这是您如何使用循环在问题(相关部分(中编写代码来反复重试卷曲请求的方式:
#include <unistd.h>
#include <curl/curl.h>
/*
* This is the maximum number of times CURL will run
*/
const int max_attempts = 5;
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);
for (int i = 1; i <= max_attempts &&
(res = curl_easy_perform(curl)) != CURLE_OK; i++) {
/*
* At this point, you would sleep
* for some seconds between requests
*/
const int sleep_secs = 1;
sleep(sleep_secs);
}
// As others have mentioned, you should delete this line:
//curl_slist_free_all(LibcurlHeaders);
if (res != CURLE_OK) {
// The max retries have all failed
LibcurlError(curl_easy_strerror(res), host);
}
else {
// The request has succeeded in the first `max_retries` attempts
// ...
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
相关文章:
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 避免模板类中的重定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- C++ 预期的左大括号以及重定义错误
- Q 斯坦达项重定义错误
- VC++ C2011 重定义错误 - 未使用的头文件
- 为什么与函数相比,链接阶段没有类重定义错误?
- 编译时出现重定义错误
- 类型定义重定义错误
- 类类型重定义错误
- C libcurl错误重试
- 如何修复带有命名空间的标头中常量的重定义错误?
- C++ 结构类型重定义错误
- 类型重定义错误(使用标头保护)
- 为什么当循环再次运行时,此输出"请重试"?
- 将代码移动到另一个标头后出现重定义错误
- 网络tcp套接字应用程序重试方法
- 正在重试包含同一异常的exception_ptr的多个实例
- 未收到确认时如何执行数据包重试
- 将成员枚举变量传递给类构造函数时出现无效的重定义错误