Преглед изворни кода

1、新增函数:ParseURL、ParseURLAndEncode
2、修复下载文件时,包含中文字符的Bug(下载的地址中的参数未经过编码处理);

scbc.sat2 пре 5 година
родитељ
комит
e15af28ce1
3 измењених фајлова са 92 додато и 13 уклоњено
  1. 68 0
      SATHTTP/SATHTTP/CurlClient.cpp
  2. 10 0
      SATHTTP/SATHTTP/CurlClient.h
  3. 14 13
      SATHTTP/SATHTTP/SATHTTP.cpp

+ 68 - 0
SATHTTP/SATHTTP/CurlClient.cpp

@@ -557,6 +557,74 @@ void CCurlClient::SetHeaders(const std::string headers)
 	m_headers = curl_slist_append(m_headers, headers.c_str());
 }
 
+void CCurlClient::ParseURL(std::string url, UrlPars &pars)
+{
+	if ( url.size() == 0 )
+		return;
+
+	// 找头;
+	int len = 0;
+	int npos = url.find("https://");
+	if ( npos == std::string::npos ) {
+		npos = url.find("http://");
+		if ( npos != std::string::npos)
+			len = _tcslen("http://");
+	} else {
+		len = _tcslen("https://");
+	}
+
+	npos = url.find("/", len);
+	if ( npos == std::string::npos )
+		return;
+	pars.host = url.substr(0, npos);
+
+	// 找api;
+	len = npos+1;
+	npos = url.find("?", len);
+	if ( npos == std::string::npos )
+		return;
+	pars.api = url.substr(len, npos-len);
+
+	// 找pars;
+	len = npos + 1;
+	std::string strPars;
+	url = url.substr(npos+1);	
+	while ( url.size() ) {
+		if ( (npos = url.find("&")) != std::string::npos ) {
+			strPars = url.substr(0, npos);
+			// 切换另一组;
+			url = url.substr(npos+1);
+
+			npos = strPars.find("=");
+			if ( npos != std::string::npos )
+				pars.pars.insert(std::pair<std::string,std::string>(strPars.substr(0, npos), strPars.substr(npos+1)));
+		} else {
+			strPars = url;
+			npos = strPars.find("=");
+			if ( npos != std::string::npos )
+				pars.pars.insert(std::pair<std::string,std::string>(strPars.substr(0, npos), strPars.substr(npos+1)));
+
+			// 结束;
+			url = "";
+		}
+	}
+}
+
+std::string CCurlClient::ParseURLAndEncode(std::string url)
+{
+	UrlPars pars;
+	ParseURL(url, pars);
+	url = pars.host + "/" + pars.api + "?";
+	for ( std::map<std::string,std::string>::iterator it = pars.pars.begin(); it != pars.pars.end(); ) {
+		it->second = CharEncoding::EnCode_UTF8URL(it->second.c_str());
+		url.append(it->first+"="+it->second);
+		if ( (++it) != pars.pars.end() )
+			url.append("&");
+	}
+
+	return url;
+}
+
 #if 0
 bool CCurlClient::Download(const std::string& url, const std::string& path, long time_out /* = 3000 */)
 {

+ 10 - 0
SATHTTP/SATHTTP/CurlClient.h

@@ -18,6 +18,14 @@ typedef wstring TString;
 #pragma once
 
 
+typedef struct _URL_PARS_ 
+{
+	std::string host;
+	std::string api;
+	std::map<std::string, std::string> pars;
+}UrlPars, *pUrlPars;
+
+
 class  CCurlClient
 {
 public:
@@ -72,6 +80,8 @@ public:
 	int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
 	int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
 
+	static void ParseURL(std::string url, UrlPars &pars);
+	static std::string ParseURLAndEncode(std::string url);
 	// Ò»´ÎÐÔÏÂÔØ;
 	bool Download(const std::string& url, const std::string& path, long time_out = 3000);
 	// ¶ÏµãÏÂÔØ;

+ 14 - 13
SATHTTP/SATHTTP/SATHTTP.cpp

@@ -66,19 +66,15 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 	}
 	else
 	{
-		std::string host = "http://10.118.158.198:8090";
-		SATHTTP::STSaveLogReq stSaveLogReq;
-		SATHTTP::STSaveLogResp stSaveLogResp;
-		std::string url = host + "/btc_execute_se/ajaxInteractiveManage!saveCaseOrTaskLog.action";
-
-		// 20191219092809213
-		stSaveLogReq.strUserId = "7";
-		stSaveLogReq.strExecuteId = "19";
-		stSaveLogReq.strCaseId = "1569";
-		stSaveLogReq.strFileType = "caseLogFile";
-		stSaveLogReq.strTaskId = "4220";
-		stSaveLogReq.strUploads = "D:\\SAT\\log.txt";
-		SaveCaseOrTaskLog(url, stSaveLogReq, stSaveLogResp);
+		UrlPars ups;
+		std::string host = "10.118.159.132:8580/btc_task_se/downLoadFile!downLoadFile.action?fileDir=/RT_2841/预抄写_RT2841/DVB&fileName=A5600007AAB_DVB_DTV_EPG.py";
+		CCurlClient::ParseURL(host, ups);
+
+		for ( std::map<std::string,std::string>::iterator it = ups.pars.begin(); it != ups.pars.end(); it++ ) {
+			it->second = CharEncoding::EnCode_UTF8URL(it->second.c_str());
+		}
+
+		printf("----\n");
 	}
 
 	system("pause");
@@ -948,6 +944,11 @@ SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::stri
 										// 创建目录;
 										if ( MKDIR(strSaveDir.c_str()) ) {
 											// 同时下载脚本目录到指定目录中;
+#ifdef _DEBUG
+											OutputDebugString((stScriptUrlResp.strURL+"\n").c_str());
+#endif
+											// url地址可能包含中文,此处需要转码;
+											stScriptUrlResp.strURL = CCurlClient::ParseURLAndEncode(stScriptUrlResp.strURL);
 											if ( g_curl.Download(stScriptUrlResp.strURL, strSaveDir, 20000) ) {
 												stScriptUrlResp._strFileName = strFileName.substr(0, strFileName.find_first_of('.'));
 												stScriptUrlResp._strScripFile = strSaveDir;