MFC使用HttpGet和HttpPost方法与服务器通信
2017-07-17 14:40
381 查看
处理过程封装到CHttpClient类中,同时支持http和https
HttpClient.h
[cpp] view
plain copy
//////////////////////////////////// HttpClient.h
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <afxinet.h>
#include <string>
using namespace std;
#define IE_AGENT _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
// 操作成功
#define SUCCESS 0
// 操作失败
#define FAILURE 1
// 操作超时 www.it165.net
#define OUTTIME 2
class CHttpClient
{
public:
CHttpClient(LPCTSTR strAgent = IE_AGENT);
virtual ~CHttpClient(void);
int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
private:
int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
void Clear();
private:
CInternetSession *m_pSession;
CHttpConnection *m_pConnection;
CHttpFile *m_pFile;
};
#endif // HTTPCLIENT_H
HttpClient.cpp
[cpp] view
plain copy
////////////////////////////////// HttpClient.cpp
#include "StdAfx.h"
#include "HttpClient.h"
#include "yazuoLog.h"
#define BUFFER_SIZE 1024
#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
CHttpClient::CHttpClient(LPCTSTR strAgent)
{
m_pSession = new CInternetSession(strAgent);
m_pConnection = NULL;
m_pFile = NULL;
}
CHttpClient::~CHttpClient(void)
{
Clear();
if(NULL != m_pSession)
{
m_pSession->Close();
delete m_pSession;
m_pSession = NULL;
}
}
void CHttpClient::Clear()
{
if(NULL != m_pFile)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
}
if(NULL != m_pConnection)
{
m_pConnection->Close();
delete m_pConnection;
m_pConnection = NULL;
}
}
int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
return 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));
//DWORD dwFlags;
//m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
//dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
////set web server option
//m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
m_pFile->AddRequestHeaders("Accept: *,*/*");
m_pFile->AddRequestHeaders("Accept-Language: zh-cn");
m_pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
m_pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
CString cs(pUnicode);
delete []pUnicode;
pUnicode = NULL;
strResponse = cs;
Clear();
}
catch (CInternetException* e)
{
Clear();
DWORD dwErrorCode = e->m_dwError;
e->Delete();
DWORD dwError = GetLastError();
PRINT_LOG("dwError = %d", dwError, 0);
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
return OUTTIME;
}
else
{
return FAILURE;
}
}
return SUCCESS;
}
int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("GET", strUrl, strPostData, strResponse);
}
int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("POST", strUrl, strPostData, strResponse);
}
HttpClient.h
[cpp] view
plain copy
//////////////////////////////////// HttpClient.h
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <afxinet.h>
#include <string>
using namespace std;
#define IE_AGENT _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
// 操作成功
#define SUCCESS 0
// 操作失败
#define FAILURE 1
// 操作超时 www.it165.net
#define OUTTIME 2
class CHttpClient
{
public:
CHttpClient(LPCTSTR strAgent = IE_AGENT);
virtual ~CHttpClient(void);
int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
private:
int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
void Clear();
private:
CInternetSession *m_pSession;
CHttpConnection *m_pConnection;
CHttpFile *m_pFile;
};
#endif // HTTPCLIENT_H
HttpClient.cpp
[cpp] view
plain copy
////////////////////////////////// HttpClient.cpp
#include "StdAfx.h"
#include "HttpClient.h"
#include "yazuoLog.h"
#define BUFFER_SIZE 1024
#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
CHttpClient::CHttpClient(LPCTSTR strAgent)
{
m_pSession = new CInternetSession(strAgent);
m_pConnection = NULL;
m_pFile = NULL;
}
CHttpClient::~CHttpClient(void)
{
Clear();
if(NULL != m_pSession)
{
m_pSession->Close();
delete m_pSession;
m_pSession = NULL;
}
}
void CHttpClient::Clear()
{
if(NULL != m_pFile)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
}
if(NULL != m_pConnection)
{
m_pConnection->Close();
delete m_pConnection;
m_pConnection = NULL;
}
}
int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
return 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));
//DWORD dwFlags;
//m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
//dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
////set web server option
//m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
m_pFile->AddRequestHeaders("Accept: *,*/*");
m_pFile->AddRequestHeaders("Accept-Language: zh-cn");
m_pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
m_pFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
CString cs(pUnicode);
delete []pUnicode;
pUnicode = NULL;
strResponse = cs;
Clear();
}
catch (CInternetException* e)
{
Clear();
DWORD dwErrorCode = e->m_dwError;
e->Delete();
DWORD dwError = GetLastError();
PRINT_LOG("dwError = %d", dwError, 0);
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
return OUTTIME;
}
else
{
return FAILURE;
}
}
return SUCCESS;
}
int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("GET", strUrl, strPostData, strResponse);
}
int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
return ExecuteRequest("POST", strUrl, strPostData, strResponse);
}
相关文章推荐
- MFC使用HttpGet和HttpPost方法与服务器通信
- MFC使用HttpGet和HttpPost方法与服务器通信
- MFC使用HttpGet和HttpPost方法与服务器通信
- MFC使用HttpGet和HttpPost方法与服务器通信
- MFC使用HttpGet和HttpPost方法与服务器通信
- Java使用HTTP的get方法读取网络数据、使用post方法与网络交互通信
- MFC中Http通信Post和Get方法实现
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- HttpClient GET POST DELETE方法使用
- Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用
- Android HTTP实例 使用GET方法和POST方法发送请求(通过Apache接口实现)
- Android HTTP实例 使用GET方法和POST方法发送请求
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
- Android HTTP实例 使用GET方法和POST方法发送请求
- Symfony2CookBook:如何在路由中使用GET和POST以外的HTTP方法
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- Android HTTP实例 使用GET方法和POST方法发送请求
- Android HTTP实例 使用GET方法和POST方法发送请求