1

我在多线程环境中使用 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 请求(约 3% 的请求)的 HTTP 响应超时。知道超时的原因是什么吗?我假设 http 服务器的响应时间不应超过 20 秒。

4

1 回答 1

0

CURLOPT_TIMEOUT 包含了http请求的所有时间,你传输过大数据吗?

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

于 2013-12-11T08:29:53.637 回答