資源簡介
MFC使用H偷偷跑Client的GET和POST方法
MFC使用H偷偷跑Client的GET和POST方法

代碼片段和文件信息
#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(pUnicode0(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF80strRawResponse.c_str()-1?pUnicodeunicodeLen);
CString?cs(pUnicode);
delete?[]pUnicode;?
pUnicode?=?NULL;
strResponse?=?cs;
Clear();
}
catc
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????3690??2013-05-29?14:52??HttpClient2.cpp
?????文件?????????859??2013-04-27?15:58??HttpClient2.h
- 上一篇:基于MFC的圖形編輯器
- 下一篇:光束法區域平差c++程序
評論
共有 條評論