概述
上一篇链接: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] = '