浏览代码

多线程环境下:curl_global_init必须要在主线程中初始化,且只能一次;

scbc.sat2 5 年之前
父节点
当前提交
87d2e94004
共有 4 个文件被更改,包括 55 次插入52 次删除
  1. 7 4
      SATHTTP/SATHTTP/CurlClient.cpp
  2. 3 1
      SATHTTP/SATHTTP/CurlClient.h
  3. 43 47
      SATHTTP/SATHTTP/SATHTTP.cpp
  4. 2 0
      SATHTTP/SATHTTP/SATHTTP.h

+ 7 - 4
SATHTTP/SATHTTP/CurlClient.cpp

@@ -5,8 +5,9 @@
 
 CCurlClient::CCurlClient(void)
 {
-	m_bDebug = FALSE;
+	m_bDebug = TRUE;
 	m_headers = NULL;
+	m_bInit = FALSE;
 }
 
 CCurlClient::~CCurlClient(void)
@@ -15,17 +16,17 @@ CCurlClient::~CCurlClient(void)
 	curl_global_cleanup(); 
 }
 
-INT CCurlClient::Initialize()
+BOOL CCurlClient::Initialize()
 {
 	// 初始化全局调用模式;
 	CURLcode res = ::curl_global_init( CURL_GLOBAL_ALL );
 	if( CURLE_OK != res ) 
 	{
 		fprintf( stderr, "curl_global_init failed: %d \n", res ); 
-		return -1;
+		return FALSE;
 	}
 
-	return 0;
+	return m_bInit = TRUE;
 }
 
 static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)   
@@ -100,6 +101,8 @@ CURLcode CCurlClient::Post(const std::string & strUrl, const std::string & strPo
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
 	// 设置头;
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
+	// 允许重定向;
+	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
 
 	// 执行POST提交;
 	res = curl_easy_perform(curl);   

+ 3 - 1
SATHTTP/SATHTTP/CurlClient.h

@@ -26,7 +26,8 @@ public:
 
 public:
 
-	INT Initialize();
+	BOOL IsInit() const {return m_bInit;};
+	BOOL Initialize();
 	/**  
 	* @brief HTTP POST请求  
 	* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com  
@@ -86,6 +87,7 @@ public:
 	}
 
 private:   
+	BOOL	m_bInit;
 	// 是否启用调试输出;
 	BOOL	m_bDebug;	
 	struct curl_slist *m_headers;

+ 43 - 47
SATHTTP/SATHTTP/SATHTTP.cpp

@@ -5,6 +5,8 @@
 #include "SATHTTP.h"
 #include <direct.h>
 
+CCurlClient g_curl;
+
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
@@ -124,6 +126,9 @@ bool MKDIR(LPCTSTR dir)
 
 SATHTTP_API int Login(std::string url, const SATParameters::STLoginReq &stLoginReq, SATParameters::STLoginResp &stLoginResp)
 {
+	if ( !g_curl.IsInit() )
+		g_curl.Initialize();
+
 	// 转化为Json;
 	cJSON *pRoot = cJSON_CreateObject();
 	if ( pRoot == NULL )
@@ -166,15 +171,14 @@ SATHTTP_API int Login(std::string url, const SATParameters::STLoginReq &stLoginR
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -289,15 +293,14 @@ SATHTTP_API int UpdateDeviceMessage(std::string url, const SATParameters::STUpda
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -387,15 +390,14 @@ SATHTTP_API int Heartbeat(std::string url, const SATParameters::STHeartbeatReq &
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -573,7 +575,8 @@ SATHTTP_API int SetResult(std::string url, const SATParameters::STNotifyJobStart
 
 	char *pText = cJSON_Print(pRoot);
 	std::string post_data = "requestMsg=";
-	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
+	post_data.append(pText);
+	//post_data.append(CharEncoding::EnCode_UTF8URL(pText));
 
 	// 释放堆内存;
 	if (pText)
@@ -584,15 +587,14 @@ SATHTTP_API int SetResult(std::string url, const SATParameters::STNotifyJobStart
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -690,7 +692,8 @@ SATHTTP_API int SetResultList(std::string url, const SATParameters::STJobProcess
 
 	char *pText = cJSON_Print(pRoot);
 	std::string post_data = "requestMsg=";
-	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
+	post_data.append(CharEncoding::EnCode_GB2312URL(pText));
+	//post_data.append(pText);
 
 	// 释放堆内存;
 	if (pText)
@@ -701,15 +704,14 @@ SATHTTP_API int SetResultList(std::string url, const SATParameters::STJobProcess
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -782,15 +784,14 @@ SATHTTP_API int GetCaseFileListUrl(std::string url, std::string strCaseId, SATPa
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -872,15 +873,14 @@ SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::stri
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -930,7 +930,7 @@ SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::stri
 										if ( MKDIR(strSaveDir.c_str()) )
 										{
 											// 同时下载脚本目录到指定目录中;
-											if ( curl.Download(stScriptUrlResp.strURL, strSaveDir, 10000) )
+											if ( g_curl.Download(stScriptUrlResp.strURL, strSaveDir, 10000) )
 											{
 												stScriptUrlResp._strFileName = strFileName.substr(0, strFileName.find_first_of('.'));
 												stScriptUrlResp._strScripFile = strSaveDir;
@@ -989,15 +989,14 @@ SATHTTP_API int StopTaskFromRunner(std::string url, std::string strInstnaceId, S
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.Post(url, post_data, reponse);
+			curlCode = g_curl.Post(url, post_data, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -1068,15 +1067,14 @@ SATHTTP_API int SaveCaseOrTaskLog(std::string url, const SATParameters::STSaveLo
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.PostFile(url, post_data, stSaveLogReq.strUploads, reponse);
+			curlCode = g_curl.PostFile(url, post_data, stSaveLogReq.strUploads, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -1166,15 +1164,14 @@ SATHTTP_API int SaveResultFile(std::string url, const SATParameters::STSaveFileR
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.PostFile(url, post_data, stSaveFileReq.strUploads, reponse);
+			curlCode = g_curl.PostFile(url, post_data, stSaveFileReq.strUploads, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{
@@ -1250,15 +1247,14 @@ SATHTTP_API int SaveResultImg(std::string url, const SATParameters::STSaveImgReq
 		cJSON_Delete(pRoot);
 	pRoot = NULL;		
 
-	CCurlClient curl;
 	std::string reponse;	
 	CURLcode curlCode = CURLE_OK;
-	if ( CURLE_OK == curl.Initialize() )
+	if ( g_curl.IsInit() )
 	{
 		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
-			curlCode = curl.Posts(url, post_data, reponse);
+			curlCode = g_curl.Posts(url, post_data, reponse);
 		else
-			curlCode = curl.PostFile(url, post_data, stSaveImgReq.strUploads, reponse);
+			curlCode = g_curl.PostFile(url, post_data, stSaveImgReq.strUploads, reponse);
 
 		if ( curlCode == CURLE_OK )
 		{

+ 2 - 0
SATHTTP/SATHTTP/SATHTTP.h

@@ -121,6 +121,8 @@ namespace SATParameters{
 		std::string _strScriptPath;
 		// Ö´ÐпªÊ¼Ê±¼ä;
 		ULONGLONG _ulStartTickCount;
+		std::string _strStartTime;
+		std::string _strEndTime;
 		__ST_CASE__()
 		{
 			_pExcutor = NULL;