【MFC】封装CHttpClient类实现HTTP请求

MFC封装CHttpClient类实现HTTP请求

环境

开发环境:Visual Studio 2010
字符集:使用多字节字符集

头文件

HttpClient.h

#define		IE_AGENT              _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")  
  
#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  

#define		HTTP_SUCCESS          0		// 操作成功  
#define		HTTP_FAILURE          1		// 操作失败 
#define		HTTP_TIMEOUT          2		// 操作超时
  
class CHttpClient  
{  
public:  
    CHttpClient(LPCTSTR strAgent = IE_AGENT);  
    virtual ~CHttpClient(void);  
  
    int                 HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);  
    int                 HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);  
    int                 HttpPut(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);  
  
    CString             UTF8AndUnicode_Convert(CString &strSource, UINT nSourceCodePage, UINT nTargetCodePage);

private:  
    int                 ExecuteRequest(int strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse);  
    void                Clear();  
  
private:  
    CInternetSession    *m_pSession;  
    CHttpConnection     *m_pConnection;  
    CHttpFile           *m_pFile;  
};  

源文件

HttpClient.cpp

// 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_PORT		nPort;  
    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; 
}

调用方法

CString             strResponse;
char                *pJsonPostData;	
	
m_HttpClient.HttpPost(_T("http://www.baidu.com/"), pJsonPostData, strResponse);
free(pJsonPostData);

pJsonPostData为HTTP请求内容,内容根据需要自己填充。

你可能感兴趣的:(MFC)