c java http,【MFC】封装CHttpClient类实现HTTP请求

// HttpClient.cpp : 定义 DLL 的初始化例程。

//

#include "stdafx.h"

#include "HttpClient.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

CHttpClient::CHttpClient(LPCTSTR strAgent)

{

m_pSession = new CInternetSession(strAgent);

m_pConnection = NULL;

m_pFile = NULL;

}

CHttpClient::~CHttpClient(void)

{

Clear();

if (m_pSession != NULL)

{

m_pSession->Close();

delete m_pSession;

m_pSession = NULL;

}

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:清除

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

void CHttpClient::Clear()

{

if (m_pFile != NULL)

{

m_pFile->Close();

delete m_pFile;

m_pFile = NULL;

}

if (m_pConnection != NULL)

{

m_pConnection->Close();

delete m_pConnection;

m_pConnection = NULL;

}

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:Http执行请求

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

int CHttpClient::ExecuteRequest(int strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)

{

int nResult;

CString strServer;

CString strObject;

DWORD dwServiceType;

INTERNET_PORTnPort;

DWORD dwRet;

CString strUtf8;

DWORD dwErrorCode;

nResult = HTTP_FAILURE;

AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);

if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)

{

return HTTP_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_pFile->AddRequestHeaders("Accept: */*,application/json");

m_pFile->AddRequestHeaders("Accept-Charset:UTF8");

m_pFile->AddRequestHeaders("Accept-Language: zh-cn;q=0.8,en;q=0.6,ja;q=0.4");

m_pFile->AddRequestHeaders("Content-Type:application/json");

m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : strlen(strPostData));

m_pFile->QueryInfoStatusCode(dwRet);

if (dwRet == HTTP_STATUS_OK)

{

nResult = HTTP_SUCCESS;

}

m_pFile->ReadString(strUtf8);

strResponse = UTF8AndUnicode_Convert(strUtf8, CP_UTF8, CP_ACP);

Clear();

}

catch (CInternetException *e)

{

Clear();

dwErrorCode = e->m_dwError;

e->Delete();

strResponse.Format(_T("CInternetException error, error code = %d!"), dwErrorCode);

if (dwErrorCode == ERROR_INTERNET_TIMEOUT)

{

return HTTP_TIMEOUT;

}

else

{

return HTTP_FAILURE;

}

}

return nResult;

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:Http获取

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)

{

return ExecuteRequest(CHttpConnection::HTTP_VERB_GET, strUrl, NULL, strResponse);

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:Http邮寄

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)

{

return ExecuteRequest(CHttpConnection::HTTP_VERB_POST, strUrl, strPostData, strResponse);

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:Http提交

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

int CHttpClient::HttpPut(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse)

{

return ExecuteRequest(CHttpConnection::HTTP_VERB_PUT, strUrl, strPostData, strResponse);

}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

//描述:Unicode与UTF-8转换

//参数:

//返回值:

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//

CString CHttpClient::UTF8AndUnicode_Convert(CString &strSource, UINT nSourceCodePage, UINT nTargetCodePage)

{

CString strTarget;

wchar_t *pWideBuf;

int nWideBufLen;

char *pMultiBuf;

int nMiltiBufLen;

int nSourceLen;

nSourceLen = strSource.GetLength();

nWideBufLen = MultiByteToWideChar(nSourceCodePage, 0, strSource, -1, NULL, 0);

pWideBuf = new wchar_t[nWideBufLen+1];

memset(pWideBuf, 0, (nWideBufLen + 1) * sizeof(wchar_t));

MultiByteToWideChar(nSourceCodePage, 0, strSource, -1, (LPWSTR)pWideBuf, nWideBufLen);

pMultiBuf = NULL;

nMiltiBufLen = WideCharToMultiByte(nTargetCodePage, 0, (LPWSTR)pWideBuf, -1, (char *)pMultiBuf, 0, NULL, NULL);

pMultiBuf = new char[nMiltiBufLen+1];

memset(pMultiBuf, 0, nMiltiBufLen + 1);

WideCharToMultiByte(nTargetCodePage, 0, (LPWSTR)pWideBuf, -1, (char *)pMultiBuf, nMiltiBufLen, NULL, NULL);

strTarget.Format(_T("%s"), pMultiBuf);

delete pWideBuf;

delete pMultiBuf;

return strTarget;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值