MFC 访问网址

.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  

.cpp文件


// HttpClient.cpp  
#include "StdAfx.h"  
#include "InternetConnection.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(_T("Accept: *,*/*"));  
       // m_pFile->AddRequestHeaders(_T("Accept-Language: zh-cn"));  
       // m_pFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));  
       // m_pFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate")); 


		m_pFile->AddRequestHeaders(_T("Accept:application/json, text/javascript, */*"));  
        m_pFile->AddRequestHeaders(_T("Accept-Language:zh-CN,zh;q=0.8"));  
        m_pFile->AddRequestHeaders(_T("Content-Type:application/json; charset=UTF-8"));  
        m_pFile->AddRequestHeaders(_T("Accept-Encoding:gzip, deflate"));

		// 这里必须使用char*数据进行上传
        m_pFile->SendRequest(NULL, 0, (LPVOID)(CW2A(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 = CW2A(cs.GetBuffer());  
  
        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(_T("GET"), strUrl, strPostData, strResponse);  
}  
  
int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)  
{  
    return ExecuteRequest(_T("POST"), strUrl, strPostData, strResponse);  
}  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值