CInternetSession类进行http请求,携带参数+数据包json
CHttpFile *MHTTP::GET(CHttpConnection *connection, CString parameter, DWORD dwServiceType)
{
// 开启一个HTTP请求
CHttpFile *pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, parameter,
NULL, 1, NULL, _T("HTTP/1.1"),
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
if (NULL == pHttpFile) {
return false;
}
// 设置HTTP请求包头
pHttpFile->AddRequestHeaders(_T("User-Agent: MYPRODUCT/1.0.0 (Windows)"));
pHttpFile->AddRequestHeaders(_T("Content-Type: application/json"));
pHttpFile->AddRequestHeaders(_T("Charset: UTF-8"));
// 发送数据
BOOL bResult = pHttpFile->SendRequest();
if (!bResult) {
CString errText;
errText.Format(L"发送数据出错,错误码:%d", bResult);
AfxMessageBox(errText);
return false;
}
return pHttpFile;
}
CHttpFile *MHTTP::POST(CHttpConnection * connection, CString parameter, DWORD dwServiceType)
{
// 开启一个HTTP请求
CHttpFile *pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_POST, parameter,
NULL, 1, NULL, _T("HTTP/1.1"),
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
if (NULL == pHttpFile) {
return NULL;
}
// 设置HTTP请求包头
pHttpFile->AddRequestHeaders(_T("User-Agent: MYPRODUCT/1.0.0 (Windows)"));
pHttpFile->AddRequestHeaders(_T("Content-Type: application/octet-stream"));
pHttpFile->AddRequestHeaders(_T("Charset: UTF-8"));
// 发送数据
BOOL bResult = pHttpFile->SendRequest(NULL, 0, getJson(), strlen(getJson()));
if (!bResult) {
CString errText;
errText.Format(L"发送数据出错,错误码:%d", bResult);
AfxMessageBox(errText);
return NULL;
}
return pHttpFile;
}
bool MHTTP::Request(CString url, UINT type)
{
bool error = false;
CString strServer;
CString parameter;
DWORD dwServiceType;
INTERNET_PORT nPort;
bool err=AfxParseURL(url, dwServiceType, strServer, parameter, nPort);
// 打开HTTP连接
CHttpConnection *pHttpConnection = session.GetHttpConnection(strServer,
dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
nPort);
if (NULL == pHttpConnection)
{
goto IS_ERROR;
}
CHttpFile *pHttpFile = nullptr;
switch (type)
{
case CHttpConnection::HTTP_VERB_POST:
pHttpFile = POST(pHttpConnection, parameter, dwServiceType);
break;
case CHttpConnection::HTTP_VERB_GET:
pHttpFile = GET(pHttpConnection, parameter, dwServiceType);
break;
}
// 查询状态
DWORD dwHttpCode = 0;
BOOL bResult = pHttpFile->QueryInfoStatusCode(dwHttpCode);
if (!bResult)
{
CString error;
error.Format(L"查询状态出错,错误码:%d", bResult);
AfxMessageBox(error);
goto IS_ERROR1;
}
// 出错的原因
if ((dwHttpCode < 200) || (dwHttpCode >= 300))
{
DWORD szBuffer;
DWORD dwBufferSize = sizeof(szBuffer);
BOOL bResult = pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize);
CString error;
error.Format(L"查询状态出错,错误码:%d", szBuffer);
AfxMessageBox(error);
}
else
{
UINT nReadBytes;
char szBuffer[40960] = {};
memset(szBuffer, 0, 40960);
// 接收响应
while ((nReadBytes = pHttpFile->Read((void*)szBuffer, 40960)) > 0)
{
_request += szBuffer;
}
error = true;
_dlg_ptr->setText(_request);
}
IS_ERROR1:
// 释放资源
if (NULL != pHttpFile) {
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
IS_ERROR:
if (NULL != pHttpConnection) {
pHttpConnection->Close();
delete pHttpConnection;
pHttpConnection = NULL;
}
return error;
}
调用
CString uri = L"http://localhost:80/api/path?key=\"key123\"&unit=\"abc456\"";
char* json("{\"name\": \"开发者\",\"code\" : \"0102\",\"number\" : \"1001\",\"key\" : \"123\"}");
setJson(json);
Request(uri, CHttpConnection::HTTP_VERB_POST);