#include "afxinet.h"
int HttpRequest(const CString & method, const CString & url, const CString& strHeader, const CString & sendData, CString & recvData, const CString & strAgent)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort = 80;
BOOL bParseUrl = AfxParseURL(url.GetString(), dwServiceType, strServer, strObject, nPort);
if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
return HTTP_FAILURE;
}
CInternetSession *pSession = new CInternetSession(NULL, 0);
CHttpConnection *pConnection = NULL;
CHttpFile *pHttpFile = NULL;
try
{
pConnection = pSession->GetHttpConnection(strServer,
dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
nPort);
pConnection->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);
pConnection->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);
pConnection->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 5000);
pConnection->SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);
pConnection->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5000);
pConnection->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pHttpFile = pConnection->OpenRequest(method.GetString(), strObject,
NULL, 1, NULL, NULL,
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
pHttpFile->AddRequestHeaders(strHeader);
USES_CONVERSION;
wchar_t *pwideBuf = nullptr;
char *pmultibuf = nullptr;
int nwidebuflen = MultiByteToWideChar(CP_ACP, 0, W2A(sendData), -1, NULL, 0);
pwideBuf = new wchar_t[nwidebuflen + 1];
memset(pwideBuf, 0, (nwidebuflen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, W2A(sendData), -1, pwideBuf, nwidebuflen);
int nmutibuflen = WideCharToMultiByte(CP_UTF8, 0, pwideBuf, -1, NULL, 0, NULL, NULL);
pmultibuf = new char[nmutibuflen + 1];
memset(pmultibuf, 0, nmutibuflen + 1);
WideCharToMultiByte(CP_UTF8, 0, pwideBuf, -1, pmultibuf, nmutibuflen, NULL, NULL);
DWORD dwOptionalLen = strlen(pmultibuf);
pHttpFile->SendRequest(NULL, 0, (LPVOID)pmultibuf, dwOptionalLen);
char szChars[1024] = { 0 };
std::string strRawResponse;
UINT nReaded = 0;
while ((nReaded = pHttpFile->Read((void*)szChars, 1024)) > 0)
{
strRawResponse.append(szChars, nReaded);
}
recvData = CString(strRawResponse.c_str());
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, pBuffer, nBufferSize * sizeof(wchar_t));
recvData = CString(pBuffer);
if (NULL != pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (NULL != pConnection)
{
pConnection->Close();
delete pConnection;
pConnection = NULL;
}
if (NULL != pSession)
{
pSession->Close();
delete pSession;
pSession = NULL;
}
}
catch (CInternetException* e)
{
if (NULL != pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (NULL != pConnection)
{
pConnection->Close();
delete pConnection;
pConnection = NULL;
}
if (NULL != pSession)
{
pSession->Close();
delete pSession;
pSession = NULL;
}
DWORD dwErrorCode = e->m_dwError;
e->Delete();
DWORD dwError = GetLastError();
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
return HTTP_OUTTIME;
}
else
{
return HTTP_FAILURE;
}
}
return HTTP_SUCCESS;
}