Parcourir la source

注意表单提交与普通的提交区别在于:CURLOPT_POST、CURLOPT_HTTPPOST

普通:
// 设置POST参数;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
// 设置POST提交方式;
res = curl_easy_setopt(curl, CURLOPT_POST, 1);

表单:
curl_formadd(……);
res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
Jeff il y a 5 ans
Parent
commit
394b634d6e

+ 185 - 5
scbc.tools/scbc.tools/CurlClient.cpp

@@ -5,7 +5,11 @@
 
 CCurlClient::CCurlClient(void)
 {
+#ifdef _DEBUG
+	m_bDebug = TRUE;
+#else
 	m_bDebug = FALSE;
+#endif
 	m_headers = NULL;
 }
 
@@ -33,27 +37,27 @@ static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void
 {   
 	if(itype == CURLINFO_TEXT)   
 	{   
-		//TRACE("[TEXT]%s\n", pData); 
+		TRACE("[TEXT]%s\n", pData); 
 		//LOG4C((LOG_WARN, "[TEXT]%s\n", pData));   
 	}   
 	else if(itype == CURLINFO_HEADER_IN)   
 	{   
-		//TRACE("[HEADER_IN]%s\n", pData); 
+		TRACE("[HEADER_IN]%s\n", pData); 
 		//LOG4C((LOG_WARN, "[HEADER_IN]%s\n", pData));
 	}   
 	else if(itype == CURLINFO_HEADER_OUT)   
 	{   
-		//TRACE("[HEADER_OUT]%s\n", pData);   
+		TRACE("[HEADER_OUT]%s\n", pData);   
 		//LOG4C((LOG_WARN, "[HEADER_OUT]%s\n", pData));
 	}   
 	else if(itype == CURLINFO_DATA_IN)   
 	{   
-		//TRACE("[DATA_IN]%s\n", pData);   
+		TRACE("[DATA_IN]%s\n", pData);   
 		//LOG4C((LOG_WARN, "[DATA_IN]%s\n", pData));
 	}   
 	else if(itype == CURLINFO_DATA_OUT)   
 	{   
-		//TRACE("[DATA_OUT]%s\n", pData);   
+		TRACE("[DATA_OUT]%s\n", pData);   
 		//LOG4C((LOG_WARN, "[DATA_OUT]%s\n", pData));
 	}   
 	return 0;   
@@ -653,6 +657,182 @@ bool CCurlClient::DownloadEx(const std::string &url, const std::string &path, lo
 	return false;
 }
 
+// 表单提交;
+// 注意与普通的post提交区别在于:CURLOPT_POST、CURLOPT_HTTPPOST
+int CCurlClient::FormPost(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out)
+{
+	CURLcode res;   
+	CURL* curl = curl_easy_init();   
+	if(NULL == curl)   
+	{   
+		return CURLE_FAILED_INIT;   
+	}  
+
+	if(m_bDebug)   
+	{   
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);   
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);   
+	}   
+
+	// 表单参数;
+	std::string post;
+	CURLFORMcode rmcode;
+	struct curl_httppost* formpost = NULL;
+	struct curl_httppost* lastptr = NULL;
+	std::map<std::string, std::string>::iterator it = form_data.begin();
+#if 0
+	for (; it != form_data.end(); )
+	{
+		post.append(it->first);
+		post.append("=");
+		post.append(it->second);
+		if ( ++it != form_data.end() )
+			post.append("&");
+		else
+			break;
+	}
+	// 设置POST参数;
+	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
+	// 设置POST提交方式;
+	res = curl_easy_setopt(curl, CURLOPT_POST, 1);  
+#else
+	for (; it != form_data.end(); it++)
+	{
+		rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
+	}
+	// 设置表单参数
+	res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
+
+	// 设置表头,表头内容可能不同
+	//m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
+	//m_headers = curl_slist_append(m_headers, "Expect:");
+	//m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
+	//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers); 
+#endif
+
+	// 设置URL地址;
+	res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());   
+	// 设置回调函数-读取;
+	res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);   
+	// 设置回调函数-写入;
+	res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);   
+	// 设置回调函数-写入的缓存区;
+	res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);   
+	// 设置;
+	res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);   
+
+	//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);   
+	//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
+	curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
+	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据 
+	curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
+	curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+
+	// 设置超时值;
+	res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
+	// 设置头;
+	res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
+
+	// 执行POST提交;
+	res = curl_easy_perform(curl);   
+
+	// 释放资源;
+	curl_easy_cleanup(curl); 
+	//curl_global_cleanup();
+	ClearHeaders(); /* free the header list */
+	// 释放表单
+	curl_formfree(formpost);
+
+	return res;   
+}
+
+int CCurlClient::FormPosts(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out)
+{
+	CURLcode res;   
+	CURL* curl = curl_easy_init();   
+	if(NULL == curl)   
+	{   
+		return CURLE_FAILED_INIT;   
+	}  
+
+	if(m_bDebug)   
+	{   
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);   
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);   
+	}   
+
+	// 表单参数;
+	std::string post;
+	CURLFORMcode rmcode;
+	struct curl_httppost* formpost = NULL;
+	struct curl_httppost* lastptr = NULL;
+	std::map<std::string, std::string>::iterator it = form_data.begin();
+#if 0
+	for (; it != form_data.end(); )
+	{
+		post.append(it->first);
+		post.append("=");
+		post.append(it->second);
+		if ( ++it != form_data.end() )
+			post.append("&");
+		else
+			break;
+	}
+	// 设置POST参数;
+	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
+	// 设置POST提交方式;
+	res = curl_easy_setopt(curl, CURLOPT_POST, 1);  
+#else
+	for (; it != form_data.end(); it++)
+	{
+		rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
+	}
+	// 设置表单参数
+	res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
+
+	// 设置表头,表头内容可能不同
+	//m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
+	//m_headers = curl_slist_append(m_headers, "Expect:");
+	//m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
+	//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers); 
+#endif
+
+	// 设置URL地址;
+	res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());   
+	// 设置回调函数-读取;
+	res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);   
+	// 设置回调函数-写入;
+	res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);   
+	// 设置回调函数-写入的缓存区;
+	res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);   
+	// 设置;
+	res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);   
+
+	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);   
+	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); 
+
+	curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
+	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据 
+	curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
+	curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 30 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+
+	// 设置超时值;
+	res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
+	// 设置头;
+	res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
+
+	// 执行POST提交;
+	res = curl_easy_perform(curl);   
+
+	// 释放资源;
+	curl_easy_cleanup(curl); 
+	//curl_global_cleanup();
+	ClearHeaders(); /* free the header list */
+	// 释放表单
+	curl_formfree(formpost);
+
+	return res;   
+}
 
 void CCurlClient::SetDebug(bool bDebug)   
 {   

+ 4 - 0
scbc.tools/scbc.tools/CurlClient.h

@@ -3,6 +3,7 @@
 
 #include <string>
 #include <vector>
+#include <map>
 using namespace std;
 #include "curl/curl.h"
 #include "curl/easy.h"
@@ -70,6 +71,9 @@ public:
 
 	bool Download(const std::string &url, const std::string &path, long time_out = 3000);
 	bool DownloadEx(const std::string &url, const std::string &path, long time_out = 3000);
+
+	int FormPost(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out = 3000);
+	int FormPosts(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out = 3000);
 public:   
 	void SetDebug(bool bDebug); 
 	void SetHeaders(const std::string headers);

+ 59 - 2
scbc.tools/scbc.tools/SDK.cpp

@@ -170,8 +170,65 @@ int CSDK::UpdateKeyReportStatus(std::string sn)
 	return bRet == TRUE ? 1 : 0;
 }
 
-int CSDK::ReportKeyCopyResults(std::string sn /* = "" */)
+int CSDK::ReportKeyCopyResults(std::string sn)
+{//http://test.admin.ota.qhmoka.com/IDManage/reportlist.do
+	if ( sn.size() == 0 )
+		return -1;
+
+	CDataImpl db;
+	if ( !db.Open() )
+		return -2;
+
+	STKeys keys;
+	int nRet = db.QueryKeyInfo(sn.c_str(), keys);
+	if ( nRet == -1)
+		return -3;
+
+	if ( nRet == 0 )
+		return 0;
+
+	if ( nRet == 1 )
+	{
+		// 上报;
+		std::string result;
+		std::map<std::string, std::string> form;
+		form.insert(std::pair<std::string,std::string>("sn", sn));
+		form.insert(std::pair<std::string,std::string>("date", keys.copy_date));
+		CCurlClient curl;
+		curl.Initialize();
+		if ( curl.FormPost("http://test.admin.ota.qhmoka.com/IDManage/reportlist.do?", form, result,3) == CURLE_OK)
+		{
+			cJSON *pJson = cJSON_Parse(result.c_str());
+			if ( pJson == NULL )
+			{
+				return -6;
+			}
+
+			std::string code, des;
+			code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valuestring: "";
+			des = cJSON_GetObjectItem(pJson, "des") ? cJSON_GetObjectItem(pJson, "des")->valuestring: "";
+			if ( _tcsicmp(code.c_str(), "1000") )
+			{
+				return -7;
+			}
+
+			// 更新上报状态;
+			nRet = db.UpdateKeyReportStatus(sn);
+			if ( nRet == 1 )
+				return 1;
+			else
+				return -8; // 上报成功,更新失败;
+		}
+
+		return -5;
+	}
+	
+	return -4;
+}
+
+int CSDK::BatchReportKeyCopyResults()
 {
+
 	return 0;
 }
 
@@ -251,7 +308,7 @@ int CSDK::GetMidInfo(std::string order, STMid &mid)
 	//content.append(Global::g_strMacs);
 	CCurlClient curl;
 	curl.Initialize();
-	if ( curl.Posts(url, content, result) == CURLE_OK)
+	if ( curl.Post(url, content, result) == CURLE_OK)
 	{
 		// 解析json字符串;
 		cJSON *pJson = cJSON_Parse(result.c_str());

+ 2 - 1
scbc.tools/scbc.tools/SDK.h

@@ -18,7 +18,8 @@ public:
 	int QueryKeyInfo(std::string sn, STKeys &keys);
 	int UpdateKeyCopyStatus(std::string sn );
 	int UpdateKeyReportStatus(std::string sn);
-	int ReportKeyCopyResults(std::string sn = "");
+	int ReportKeyCopyResults(std::string sn);
+	int BatchReportKeyCopyResults();
 
 	int GetMidInfo(std::string order, STMid &mid);
 	static int ReportDownloadStatus(std::string order);

+ 1 - 1
scbc.tools/scbc.tools/scbc.tools.cpp

@@ -315,7 +315,7 @@ int ReportKeyCopyResults(const char* lpSN)
 	if ( lpSN == NULL || lpSN[0] == '\0' )
 		return -1;
 
-	return 0;
+	return g_sdk.ReportKeyCopyResults(lpSN);
 }
 
 int QueryBidInfo(const char* lpOrder, BidInfo& binfo)

+ 1 - 1
scbc.tools/scbc.tools/scbc.tools.vcproj

@@ -90,7 +90,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
-				CommandLine="copy $(TargetDir)\$(SolutionName).lib $(SolutionDir)\test\$(ProjectName).lib /y/a"
+				CommandLine="copy $(TargetDir)\$(SolutionName).lib $(SolutionDir)\test\$(ProjectName).lib /y/a&#x0D;&#x0A;copy $(TargetPath) $(SolutionDir)\test\$(TargetFileName) /y/a&#x0D;&#x0A;copy $(SolutionDir)$(SolutionName)\TableInfo.h $(SolutionDir)\test\TableInfo.h /y/a&#x0D;&#x0A;copy $(SolutionDir)$(SolutionName)\$(SolutionName).h $(SolutionDir)\test\$(SolutionName).h  /y/a"
 			/>
 		</Configuration>
 		<Configuration

+ 8 - 2
scbc.tools/test/test.cpp

@@ -32,8 +32,14 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 		int nRet = QueryMidInfo("10");
 		printf("QueryMidInfo£º%d\n", nRet);
 
-		nRet = RemoveBidData("1");
-		printf("RemoveBidData£º%d\n", nRet);
+// 		nRet = DownloadMidData("IDL144240G");
+// 		printf("DownloadMidData£º%d\n", nRet);
+// 		Sleep(5000);
+
+		//nRet = RemoveBidData("1");
+		//printf("RemoveBidData£º%d\n", nRet);
+
+		nRet = ReportKeyCopyResults("TEST-3311-8811");
 	}
 
 	system("pause");