Przeglądaj źródła

下载文件有问题,待处理;

scbc.sat2 5 lat temu
rodzic
commit
3f24f172bf
2 zmienionych plików z 69 dodań i 16 usunięć
  1. 50 0
      SATHTTP/SATHTTP/CurlClient.cpp
  2. 19 16
      SATHTTP/SATHTTP/SATHTTP.cpp

+ 50 - 0
SATHTTP/SATHTTP/CurlClient.cpp

@@ -557,6 +557,7 @@ void CCurlClient::SetHeaders(const std::string headers)
 	m_headers = curl_slist_append(m_headers, headers.c_str());
 }
 
+#if 0
 bool CCurlClient::Download(const std::string& url, const std::string& path, long time_out /* = 3000 */)
 {
 	FILE* pf = NULL;
@@ -570,6 +571,7 @@ bool CCurlClient::Download(const std::string& url, const std::string& path, long
 		CURL* curl = curl_easy_init();
 		if (NULL == curl)
 		{
+			OutputDebugString("下载文件失败,curl创建失败\n");
 			return false;
 		}
 		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
@@ -598,8 +600,53 @@ bool CCurlClient::Download(const std::string& url, const std::string& path, long
 			return true;
 	}
 
+	OutputDebugString("下载文件失败,创建文件失败\n");
+
+	return false;
+}
+#else
+bool CCurlClient::Download(const std::string& url, const std::string& path, long time_out /* = 3000 */)
+{
+	FILE* pf = NULL;
+	long file_size = 0;
+
+	CURLcode res = CURLE_GOT_NOTHING;
+	// 采用追加方式打开文件,便于实现断点续传;
+	int nErr = _tfopen_s(&pf, path.c_str(), "wb+");
+	if (pf)
+	{
+		CURL* curl = curl_easy_init();
+		if (NULL == curl)
+		{
+			OutputDebugString("下载文件失败,curl创建失败\n");
+			return false;
+		}
+		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, time_out);
+		if (url.find("https://") != std::string::npos)
+		{
+			curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
+			curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
+		}
+		curl_easy_setopt(curl, CURLOPT_WRITEDATA, pf);
+		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);//设置重定位URL,使用自动跳转,返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
+		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteFile);
+		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
+		//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // 不打印日志;
+
+		res = curl_easy_perform(curl);
+		fclose(pf);
+		curl_easy_cleanup(curl);
+
+		if (res == CURLE_OK)
+			return true;
+	}
+
+	OutputDebugString("下载文件失败,创建文件失败\n");
+
 	return false;
 }
+#endif
 
 // 参考:https://blog.csdn.net/lengyuezuixue/article/details/81987695
 bool CCurlClient::DownloadEx(const std::string& url, const std::string& path, long time_out /* = 3000 */)
@@ -677,7 +724,10 @@ size_t CCurlClient::OnGetContentLength(void* ptr, size_t size, size_t nmemb, voi
 size_t CCurlClient::OnWriteFile(const void* ptr, size_t size, size_t nmemb, void* stream)
 {
 	if (NULL == stream || NULL == ptr)
+	{
+		OutputDebugString("写入内容空\n");
 		return -1;
+	}
 
 	return fwrite(ptr, size, nmemb, (FILE*)stream);
 }

+ 19 - 16
SATHTTP/SATHTTP/SATHTTP.cpp

@@ -162,7 +162,7 @@ SATHTTP_API int Login(std::string url, const SATHTTP::STLoginReq &stLoginReq, SA
 	std::string post_data = "requestMsg=";
 	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -187,7 +187,7 @@ SATHTTP_API int Login(std::string url, const SATHTTP::STLoginReq &stLoginReq, SA
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -289,7 +289,7 @@ SATHTTP_API int UpdateDeviceMessage(std::string url, const SATHTTP::STUpdateDevi
 	std::string post_data = "requestMsg=";
 	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -314,7 +314,7 @@ SATHTTP_API int UpdateDeviceMessage(std::string url, const SATHTTP::STUpdateDevi
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -594,7 +594,7 @@ SATHTTP_API int SetResult(std::string url, const SATHTTP::STNotifyJobStartReq &s
 	post_data.append(pText);
 	//post_data.append(CharEncoding::EnCode_UTF8URL(pText));
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -619,7 +619,7 @@ SATHTTP_API int SetResult(std::string url, const SATHTTP::STNotifyJobStartReq &s
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -716,7 +716,7 @@ SATHTTP_API int SetResultList(std::string url, const SATHTTP::STJobProcessReq &s
 	post_data.append(CharEncoding::EnCode_GB2312URL(pText));
 	//post_data.append(pText);
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -741,7 +741,7 @@ SATHTTP_API int SetResultList(std::string url, const SATHTTP::STJobProcessReq &s
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -801,7 +801,7 @@ SATHTTP_API int GetCaseFileListUrl(std::string url, std::string strCaseId, SATHT
 	std::string post_data = "requestMsg=";
 	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -826,7 +826,7 @@ SATHTTP_API int GetCaseFileListUrl(std::string url, std::string strCaseId, SATHT
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -894,6 +894,9 @@ SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::stri
 	char *pText = cJSON_Print(pRoot);
 	std::string post_data = "requestMsg=";
 	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
+#ifdef _DEBUG
+	OutputDebugString((std::string(pText)+"\n").c_str());
+#endif
 
 	// 释放堆内存;
 	if (pText)
@@ -945,7 +948,7 @@ SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::stri
 										// 创建目录;
 										if ( MKDIR(strSaveDir.c_str()) ) {
 											// 同时下载脚本目录到指定目录中;
-											if ( g_curl.Download(stScriptUrlResp.strURL, strSaveDir, 10000) ) {
+											if ( g_curl.Download(stScriptUrlResp.strURL, strSaveDir, 20000) ) {
 												stScriptUrlResp._strFileName = strFileName.substr(0, strFileName.find_first_of('.'));
 												stScriptUrlResp._strScripFile = strSaveDir;
 												// 释放内存;
@@ -1016,7 +1019,7 @@ SATHTTP_API int StopTaskFromRunner(std::string url, std::string strInstnaceId, S
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -1074,7 +1077,7 @@ SATHTTP_API int SaveCaseOrTaskLog(std::string url, const SATHTTP::STSaveLogReq &
 	// POST表单提交,此处不需要转url格式,注意避坑!
 	std::string post_data = pText;
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -1099,7 +1102,7 @@ SATHTTP_API int SaveCaseOrTaskLog(std::string url, const SATHTTP::STSaveLogReq &
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());
@@ -1176,7 +1179,7 @@ SATHTTP_API int SaveResultFile(std::string url, const SATHTTP::STSaveFileReq &st
 	// POST表单提交,此处不需要转url格式,注意避坑!
 	std::string post_data = pText;
 #ifdef _DEBUG
-	OutputDebugString(CString(pText)+"\n");
+	//OutputDebugString(CString(pText)+"\n");
 #endif
 
 	// 释放堆内存;
@@ -1201,7 +1204,7 @@ SATHTTP_API int SaveResultFile(std::string url, const SATHTTP::STSaveFileReq &st
 		{
 			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
 #ifdef _DEBUG
-			OutputDebugString(CString(reponse.c_str())+"\n");
+			//OutputDebugString(CString(reponse.c_str())+"\n");
 #endif
 			// 解析返回值;
 			pRoot = cJSON_Parse(reponse.c_str());