MFC使用HttpGet和HttpPost方法与服务器通信

处理过程封装到CHttpClient类中,同时支持http和https


HttpClient.h

[cpp]  view plain  copy
  1.  HttpClient.h  
  2. #ifndef HTTPCLIENT_H  
  3. #define HTTPCLIENT_H  
  4.   
  5. #include <afxinet.h>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9. #define  IE_AGENT  _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")  
  10.   
  11. // 操作成功  
  12. #define SUCCESS        0  
  13. // 操作失败  
  14. #define FAILURE        1  
  15. // 操作超时 www.it165.net  
  16. #define OUTTIME        2  
  17.   
  18. class CHttpClient  
  19. {  
  20. public:  
  21.     CHttpClient(LPCTSTR strAgent = IE_AGENT);  
  22.     virtual ~CHttpClient(void);  
  23.   
  24.     int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);  
  25.     int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);  
  26.   
  27. private:  
  28.     int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);  
  29.     void Clear();  
  30.   
  31. private:  
  32.     CInternetSession *m_pSession;  
  33.     CHttpConnection *m_pConnection;  
  34.     CHttpFile *m_pFile;  
  35. };  
  36.   
  37. #endif // HTTPCLIENT_H  



HttpClient.cpp


[cpp]  view plain  copy
  1. // HttpClient.cpp  
  2. #include "StdAfx.h"  
  3. #include "HttpClient.h"  
  4. #include "yazuoLog.h"  
  5.   
  6. #define  BUFFER_SIZE       1024  
  7.   
  8. #define  NORMAL_CONNECT             INTERNET_FLAG_KEEP_CONNECTION  
  9. #define  SECURE_CONNECT                NORMAL_CONNECT | INTERNET_FLAG_SECURE  
  10. #define  NORMAL_REQUEST             INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE   
  11. #define  SECURE_REQUEST             NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID  
  12.   
  13. CHttpClient::CHttpClient(LPCTSTR strAgent)  
  14. {  
  15.     m_pSession = new CInternetSession(strAgent);  
  16.     m_pConnection = NULL;  
  17.     m_pFile = NULL;  
  18. }  
  19.   
  20.   
  21. CHttpClient::~CHttpClient(void)  
  22. {  
  23.     Clear();  
  24.     if(NULL != m_pSession)  
  25.     {  
  26.         m_pSession->Close();  
  27.         delete m_pSession;  
  28.         m_pSession = NULL;  
  29.     }  
  30. }  
  31.   
  32. void CHttpClient::Clear()  
  33. {  
  34.     if(NULL != m_pFile)  
  35.     {  
  36.         m_pFile->Close();  
  37.         delete m_pFile;  
  38.         m_pFile = NULL;  
  39.     }  
  40.   
  41.     if(NULL != m_pConnection)  
  42.     {  
  43.         m_pConnection->Close();  
  44.         delete m_pConnection;  
  45.         m_pConnection = NULL;  
  46.     }  
  47. }  
  48.   
  49. int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)  
  50. {  
  51.     CString strServer;  
  52.     CString strObject;  
  53.     DWORD dwServiceType;  
  54.     INTERNET_PORT nPort;  
  55.     strResponse = "";  
  56.   
  57.     AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);  
  58.   
  59.     if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)  
  60.     {  
  61.         return FAILURE;  
  62.     }  
  63.   
  64.     try  
  65.     {  
  66.         m_pConnection = m_pSession->GetHttpConnection(strServer,  
  67.             dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,  
  68.             nPort);  
  69.         m_pFile = m_pConnection->OpenRequest(strMethod, strObject,   
  70.             NULL, 1, NULL, NULL,   
  71.             (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));  
  72.   
  73.         //DWORD dwFlags;  
  74.         //m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);  
  75.         //dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;  
  76.         set web server option  
  77.         //m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);  
  78.   
  79.         m_pFile->AddRequestHeaders("Accept: *,*/*");  
  80.         m_pFile->AddRequestHeaders("Accept-Language: zh-cn");  
  81.         m_pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");  
  82.         m_pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");  
  83.   
  84.         m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));  
  85.   
  86.         char szChars[BUFFER_SIZE + 1] = {0};  
  87.         string strRawResponse = "";  
  88.         UINT nReaded = 0;  
  89.         while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)  
  90.         {  
  91.             szChars[nReaded] = '\0';  
  92.             strRawResponse += szChars;  
  93.             memset(szChars, 0, BUFFER_SIZE + 1);  
  94.         }  
  95.   
  96.         int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);  
  97.         WCHAR *pUnicode = new WCHAR[unicodeLen + 1];  
  98.         memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
  99.   
  100.         MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);  
  101.         CString cs(pUnicode);  
  102.         delete []pUnicode;   
  103.         pUnicode = NULL;  
  104.   
  105.         strResponse = cs;  
  106.   
  107.         Clear();  
  108.     }  
  109.     catch (CInternetException* e)  
  110.     {  
  111.         Clear();  
  112.         DWORD dwErrorCode = e->m_dwError;  
  113.         e->Delete();  
  114.   
  115.         DWORD dwError = GetLastError();  
  116.   
  117.         PRINT_LOG("dwError = %d", dwError, 0);  
  118.   
  119.         if (ERROR_INTERNET_TIMEOUT == dwErrorCode)  
  120.         {  
  121.             return OUTTIME;  
  122.         }  
  123.         else  
  124.         {  
  125.             return FAILURE;  
  126.         }  
  127.     }  
  128.     return SUCCESS;  
  129. }  
  130.   
  131. int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)  
  132. {  
  133.     return ExecuteRequest("GET", strUrl, strPostData, strResponse);  
  134. }  
  135.   
  136. int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)  
  137. {  
  138.     return ExecuteRequest("POST", strUrl, strPostData, strResponse);  
  139. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值