上一篇链接: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
#include
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
在strUrl中包含了我需要发送到服务端得ip、端口号,以及打包的json数据,后面使用HttpGet请求,此时就可以获取到服务端响应的json数据,保存在了strResponse中,这些都是在HttpClient.cpp中实现的,后面就是进行解析了,因为每个人写法习惯不一样,这里我只说下其中的conten是key值,后面的是value,至于定义key的内容,需要你们自己商量,或者你在了解下json格式,然后将你解析后数据再发送给所需要的部分就完成了json的解析发送。至此完成所有过程。
以下链接是对上一篇以及这一篇代码的总体实现,因为自身使用Unicode编码转换的,其中有我详细使用的http以及json数据的解析,创作不易,有兴趣可以看下。
链接:https://download.csdn.net/download/hfuu1504011020/11057233