上一篇鏈接:https://blog.csdn.net/hfuu1504011020/article/details/88785448
上一篇中說到Unicode轉utf8格式的過程,其中談及到http以及json數據的解析,json解析其實也沒啥說的,網上一大推,後面大概介紹下使用,本文着重介紹下c++客戶端實現的http連接服務端並返回json數據的過程。
(重點)這裏http的連接以及獲取json數據過程使用的是多字節編碼實現的,直接可以將其轉爲utf8編碼存入mysql中。之前我因爲使用的是Unicode編碼,在本文代碼的基礎上再加入上一篇的編碼轉換也是可以實現Unicode轉utf8格式過程。
我們看下http頭文件所包含的參數以及函數
//////////////////////////////////// HttpClient.h
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <afxinet.h>
#include <string>
using namespace std;
#define IE_AGENT _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
// 操作成功
#define SUCCESS 0
// 操作失敗
#define FAILURE 1
// 操作超時 www.it165.net
#define OUTTIME 2
class CHttpClient
{
public:
CHttpClient(LPCTSTR strAgent = IE_AGENT);
virtual ~CHttpClient(void);
/*static wchar_t* ANSIToUnicode(const char* str);
static char* UnicodeToANSI(const wchar_t* str);
static char* UnicodeToUTF8(const wchar_t* str);
*/ //在下面的ExecuteRequest函數中處理了多字節轉utf8的方法,不需要再使用這三個函數,這裏註釋
// 的三句轉碼總覺得在使用時有問題,代碼我一併貼出,供大家查找問題
int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
private:
int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
void Clear();
private:
CInternetSession *m_pSession;
CHttpConnection *m_pConnection;
CHttpFile *m_pFile;
};
#endif // HTTPCLIENT_H
以上是http實現的頭文件,以下爲.cpp中代碼實現過程,內容有點多,若只是使用的話粘貼複製即可
////////////////////////////////// HttpClient.cpp
#include "StdAfx.h"
#include "HttpClient.h"
#define BUFFER_SIZE 1024
#define NORMAL_CONNECT INTERNET_FLAG_KEEP_CONNECTION
#define SECURE_CONNECT NORMAL_CONNECT | INTERNET_FLAG_SECURE
#define NORMAL_REQUEST INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE
#define SECURE_REQUEST NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
CHttpClient::CHttpClient(LPCTSTR strAgent)
{
m_pSession = new CInternetSession(strAgent);
m_pConnection = NULL;
m_pFile = NULL;
}
CHttpClient::~CHttpClient(void)
{
Clear();
if(NULL != m_pSession)
{
m_pSession->Close();
delete m_pSession;
m_pSession = NULL;
}
}
void CHttpClient::Clear()
{
if(NULL != m_pFile)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
}
if(NULL != m_pConnection)
{
m_pConnection->Close();
delete m_pConnection;
m_pConnection = NULL;
}
}
int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
DWORD dwFlags;
DWORD dwStatus = 0;
DWORD dwStatusLen = sizeof(dwStatus);
CString strLine;
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
try
{
if (dwServiceType == AFX_INET_SERVICE_HTTP)
{
m_pConnection = m_pSession->GetHttpConnection(strServer,NORMAL_CONNECT,nPort);
}
else
{
m_pConnection = m_pSession->GetHttpConnection(strServer, INTERNET_FLAG_SECURE, nPort,
NULL, NULL);
}
if(m_pConnection)
{
if (dwServiceType == AFX_INET_SERVICE_HTTP)
{
m_pFile = m_pConnection->OpenRequest(strMethod, strObject,
NULL, 1, NULL, NULL, NORMAL_REQUEST);
}
else
{
m_pFile = (CHttpFile*)m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1,
NULL, NULL,
INTERNET_FLAG_SECURE |
INTERNET_FLAG_EXISTING_CONNECT |
INTERNET_FLAG_RELOAD |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID
);
//get web server option
m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
//set web server option
m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
}
m_pFile->AddRequestHeaders("Accept: *,*/*");
m_pFile->AddRequestHeaders("Accept-Language: zh-cn");
m_pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
m_pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
if(m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData)))
{
//get response status if success, return 200
if (dwServiceType != AFX_INET_SERVICE_HTTP)
{
m_pFile->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE,
&dwStatus, &dwStatusLen, 0);
}
// while(m_pFile->ReadString(strLine))
// {
// //m_strHtml += Convert(strLine, CP_ACP);
// m_strHtml += strLine + char(13) + char(10);
// }
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pUnicode,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的運用
char *psText; // psText爲char*的臨時數組,作爲賦值給std::string的中間變量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,pUnicode,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次運用
string szDst = psText;// std::string賦值
delete []psText;// psText的清除
strResponse = szDst;
// char *ansi_str = UnicodeToUTF8(pUnicode);
//
// string str = ansi_str;
// free(ansi_str);
//
// CString cs(str.c_str());
delete []pUnicode;
pUnicode = NULL;
// strResponse = cs;
Clear();
}
else
{
return FAILURE;
}
}
else
{
return FAILURE;
}
}
catch (CInternetException* e)
{
Clear();
DWORD dwErrorCode = e->m_dwError;
e->Delete();
DWORD dwError = GetLastError();
// PRINT_LOG("dwError = %d", dwError, 0);
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
return OUTTIME;
}
else
{
return FAILURE;
}
}
return SUCCESS;
}
int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("GET", strUrl, strPostData, strResponse);
}
int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("POST", strUrl, strPostData, strResponse);
}
wchar_t* CHttpClient::ANSIToUnicode(const char* str)
{
int textlen;
wchar_t * result;
textlen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
result = (wchar_t *)malloc((textlen + 1) * sizeof(wchar_t));
memset(result, 0, (textlen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)result, textlen);
return result;
}
char* CHttpClient::UnicodeToUTF8(const wchar_t* str)
{
char* result;
int textlen;
textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
result = (char *)malloc((textlen + 1) * sizeof(char));
memset(result, 0, sizeof(char) * (textlen + 1));
WideCharToMultiByte(CP_UTF8, 0, str, -1, result, textlen, NULL, NULL);
return result;
}
char* CHttpClient::UnicodeToANSI(const wchar_t* str)
{
char* result;
int textlen;
textlen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
result = (char *)malloc((textlen + 1) * sizeof(char));
memset(result, 0, sizeof(char) * (textlen + 1));
WideCharToMultiByte(CP_ACP, 0, str, -1, result, textlen, NULL, NULL);
return result;
}
仔細觀察上面所說函數,就可以看到在ExecuteRequest函數中處理了多字節轉爲utf8格式,後面單獨寫了幾種其他轉碼方式,這幾種方式在轉碼時總覺得有問題,詳細介紹大家參考以上內容。
後面說下json數據,這裏我們的http使用的是get請求方式,貼一份我查詢會議的代碼,內容相對全些,後面再介紹。
int CDatabaseManage::QueryMRInBooked( CString roomId,CString data ,HWND hwnd)
{
TMRBookedInfo tmrBookedInfo;
TMRBookedInfoArray tmrBookedInfoArray;
tmrBookedInfoArray.clear();
UINT uiRet = -1;
if (m_httpCtrl == NULL)
{
m_httpCtrl = new CHttpClient();
}
CString strUrl,strPostData ;
strUrl.Format(_T("http://此處是ip地址:端口號/QueryConferenceRecord?content={\"Conference_Index\":\"%s\",\"RecordTime\":\"%s\"}"),roomId,data);
string strResponse="";
uiRet = m_httpCtrl->HttpGet(strUrl,strPostData,strResponse);
if ((uiRet == SUCCESS) &&(!strResponse.empty()))
{
Json::Value root;
Json::Reader reader;
if (reader.parse(strResponse,root,false))
{
if (root.isMember("content"))
{
Json::Value jsResult=root["content"];
if (jsResult.size()==0)
{
return 0;
}
uiRet = jsResult.size();
for(int i=0;i<jsResult.size();i++)
{
if (jsResult[i].isMember("Index"))
{
tmrBookedInfo.uiIdx = jsResult[i]["Index"].asInt();
}
if (jsResult[i].isMember("Subject"))
{
tmrBookedInfo.strObj = jsResult[i]["Subject"].asCString();
}
if (jsResult[i].isMember("BeginTime"))
{
tmrBookedInfo.uiBeginTime = jsResult[i]["BeginTime"].asCString();
}
if (jsResult[i].isMember("EndTime"))
{
tmrBookedInfo.uiEndTime = jsResult[i]["EndTime"].asCString();
}
if (jsResult[i].isMember("UserName"))
{
tmrBookedInfo.uiUserName =jsResult[i]["UserName"].asCString();
}
tmrBookedInfoArray.push_back(tmrBookedInfo);
}
::SendMessage(hwnd,CM_SHOWRECORD,(WPARAM)&tmrBookedInfoArray,0);
}
}
}
return uiRet;
}
在strUrl中包含了我需要發送到服務端得ip、端口號,以及打包的json數據,後面使用HttpGet請求,此時就可以獲取到服務端響應的json數據,保存在了strResponse中,這些都是在HttpClient.cpp中實現的,後面就是進行解析了,因爲每個人寫法習慣不一樣,這裏我只說下其中的conten是key值,後面的是value,至於定義key的內容,需要你們自己商量,或者你在瞭解下json格式,然後將你解析後數據再發送給所需要的部分就完成了json的解析發送。至此完成所有過程。
以下鏈接是對上一篇以及這一篇代碼的總體實現,因爲自身使用Unicode編碼轉換的,其中有我詳細使用的http以及json數據的解析,創作不易,有興趣可以看下。
鏈接:https://download.csdn.net/download/hfuu1504011020/11057233