2013-03-11 98 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傳輸操作執行的最大時間的參數(以秒爲單位)。通常,名稱查找可能需要相當長的時間,並且將操作限制在不到幾分鐘的時間內可能會導致完全正常的操作中止。