张洋 4 rokov pred
rodič
commit
56418bfc28

+ 12 - 12
zhipuzi_pos_windows/network/CHttpClient.cpp

@@ -57,7 +57,7 @@ static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
 	return nmemb;
 }
 
-int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, CZhipuziHttpProxy proxy)
+int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, CZhipuziHttpProxy proxy, int timeout)
 {
 	CURLcode res;
 	CURL* curl = curl_easy_init();
@@ -93,14 +93,14 @@ int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, s
 	}
 
 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;
 }
 
-int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, CZhipuziHttpProxy proxy)
+int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, CZhipuziHttpProxy proxy, int timeout)
 {
 	CURLcode res;
 	CURL* curl = curl_easy_init();
@@ -138,8 +138,8 @@ int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, CZhi
 	* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
 	*/
 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;
@@ -148,7 +148,7 @@ int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, CZhi
 /*
  *返回值说明 https://blog.csdn.net/u011857683/article/details/53069268
  **/
-int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy)
+int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy, int timeout)
 {
 	CURLcode res;
 	CURL* curl = curl_easy_init();
@@ -200,14 +200,14 @@ int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost,
 		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
 	}
 
-	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;
 }
 
-int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy)
+int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy, int timeout)
 {
 	CURLcode res;
 	CURL* curl = curl_easy_init();
@@ -254,8 +254,8 @@ int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, con
 		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
 	}
 
-	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;

+ 4 - 4
zhipuzi_pos_windows/network/CHttpClient.h

@@ -16,7 +16,7 @@ public:
 	* @param strResponse 输出参数,返回的内容
 	* @return 返回是否Post成功
 	*/
-	int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, CZhipuziHttpProxy proxy = CZhipuziHttpProxy());
+	int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, CZhipuziHttpProxy proxy = CZhipuziHttpProxy(), int timeout = 5);
 
 	/**
 	* @brief HTTP GET请求
@@ -24,7 +24,7 @@ public:
 	* @param strResponse 输出参数,返回的内容
 	* @return 返回是否Post成功
 	*/
-	int Get(const std::string & strUrl, std::string & strResponse, CZhipuziHttpProxy proxy = CZhipuziHttpProxy());
+	int Get(const std::string & strUrl, std::string & strResponse, CZhipuziHttpProxy proxy = CZhipuziHttpProxy(), int timeout = 5);
 
 	/**
 	* @brief HTTPS POST请求,无证书版本
@@ -34,7 +34,7 @@ public:
 	* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
 	* @return 返回是否Post成功
 	*/
-	int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, CZhipuziHttpProxy proxy = CZhipuziHttpProxy());
+	int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, CZhipuziHttpProxy proxy = CZhipuziHttpProxy(), int timeout = 5);
 
 	/**
 	* @brief HTTPS GET请求,无证书版本
@@ -43,7 +43,7 @@ public:
 	* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
 	* @return 返回是否Post成功
 	*/
-	int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, CZhipuziHttpProxy proxy = CZhipuziHttpProxy());
+	int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, CZhipuziHttpProxy proxy = CZhipuziHttpProxy(), int timeout = 5);
 
 public:
 	void SetDebug(bool bDebug);

+ 9 - 1
zhipuzi_pos_windows/network/CZhipuziHttpClient.cpp

@@ -212,8 +212,16 @@ bool CZhipuziHttpClient::Request(std::string url, std::map<string, string> param
 
 	//LOG_INFO("postString:" << postString.c_str());
 
+	int timeout = 5;
+	
+	if (url == "/goods/getallgoods")
+	{
+		//初始化所有商品,把超时时间延长一点
+		timeout = 30;
+	}
+
 	CHttpClient m_httpClient;
-	int ret = m_httpClient.Posts(m_yewu_url + url, postString, response, NULL, m_http_proxy);
+	int ret = m_httpClient.Posts(m_yewu_url + url, postString, response, NULL, m_http_proxy, timeout);
 
 	//LOG_INFO("response:" << response.c_str());