#include "../pch.h"
#include "HttpClient.h"
#define BUFFER_SIZE 102400
#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;
token = "";
connectTimeoutInMilliseconds = 30;
recvTimeoutInMilliseconds = 30;
}
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)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
logger.INFO_F(__FUNCTION__ " "+ string(strMethod)+" "+string(strUrl)+" "+string(PS(token)));
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
logger.ERROR_F("URL format error:"+string(strUrl));
return FAILURE;
}
try
{
m_pConnection = m_pSession->GetHttpConnection(strServer,
dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
nPort);
m_pFile = m_pConnection->OpenRequest(strMethod, strObject,
NULL, 1, NULL, NULL,
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
m_pConnection->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, connectTimeoutInMilliseconds);
m_pFile->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, recvTimeoutInMilliseconds);
m_pFile->AddRequestHeaders("Accept: *,*/*");
m_pFile->AddRequestHeaders("content-type: application/json");
m_pFile->AddRequestHeaders("Authorization: Bearer "+token);
m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
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);
CString cs(pUnicode);
delete[]pUnicode;
pUnicode = NULL;
strResponse = cs;
Clear();
}
catch (CInternetException* e)
{
logger.ERROR_F(__FUNCTION__"dwError:" + to_string(e->m_dwError));
Clear();
DWORD dwErrorCode = e->m_dwError;
e->Delete();
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
return OUTTIME;
}
else
{
return FAILURE;
}
}
logger.DEBUG_F("strResponse:"+strResponse);
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);
}
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cctype>
std::string url_encode(const std::string& value) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto c : value) {
if (isalnum((unsigned char)c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int((unsigned char)c);
escaped << std::nouppercase;
}
return escaped.str();
}
CString CHttpClient::EscapeUrl(CString url) {
CString escapedUrl = url_encode(url.GetBuffer()).c_str();
return escapedUrl;
}
#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
#define OUTTIME 2
class CHttpClient
{
public:
CHttpClient(LPCTSTR strAgent = IE_AGENT);
virtual ~CHttpClient(void);
int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string& strResponse);
CString EscapeUrl(CString url);
public:
CString token;
DWORD connectTimeoutInMilliseconds;
DWORD recvTimeoutInMilliseconds;
private:
void Clear();
private:
CInternetSession *m_pSession;
CHttpConnection *m_pConnection;
CHttpFile *m_pFile;
};
#endif