libcurl http post timeout

时间:2013-03-11 07:28:16

标签: libcurl

我在多线程环境中使用curl版本7.15.5。每个线程都在初始化并释放自己的curl对象。下面是为每个线程执行的代码:

CURL* curl = curl_easy_init();

tRespBuffer respBuffer = {NULL, 0};
char errorBuf[CURL_ERROR_SIZE +1];
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: text/xml; charset=gbk");
headers = curl_slist_append(headers, "Expect:");

curl_easy_setopt(curl, CURLOPT_URL, url_);

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,encr.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,strlen(encr.c_str()));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpSmsServer::processHttpResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&respBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20);  // wait for 20 seconds before aborting the transacttion
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuf);  // error returned if any..
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  // No signals allowed in case of multithreaded apps

res = curl_easy_perform(curl);

curl_slist_free_all(headers);
curl_easy_cleanup(curl);

所有四个主题都是同时向http服务器发布数据。我看到一些POST请求的HTTP响应超时(约占请求的3%)。知道什么可能是超时的原因?我假设http服务器不应该花费超过20秒的时间来回复。

1 个答案:

答案 0 :(得分:0)

CURLOPT_TIMEOUT包括http请求的所有时间,您是否已传输大量数据?

CURLOPT_TIMEOUT:传递一个long参数,其中包含允许libcurl传输操作采用的最长时间(以秒为单位)。通常,名称查找可能需要相当长的时间,并且将操作限制在不到几分钟的时间内会导致完全正常的操作中止。

相关问题