2013-03-11 97 views
1

我在多线程环境中使用curl版本7.15.5。每个线程正在初始化并释放它自己的卷曲对象。以下是为每个线程执行的代码:libcurl http post timeout

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秒来回应。

回答

0

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

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