#include "StdAfx.h" #include "CMyHttpClient.h" #include //#include "ShardMainWinHandle.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 CMyHttpClient::CMyHttpClient(LPCTSTR strAgent) : MySendHeader(_T("")) { m_pSession = new CInternetSession(strAgent); m_pConnection = NULL; m_pHttpFile = NULL; } CMyHttpClient::~CMyHttpClient(void) { Clear(); if (NULL != m_pSession) { m_pSession->Close(); delete m_pSession; m_pSession = NULL; } } void CMyHttpClient::Clear() { if (NULL != m_pHttpFile) { m_pHttpFile->Close(); delete m_pHttpFile; m_pHttpFile = NULL; } if (NULL != m_pConnection) { m_pConnection->Close(); delete m_pConnection; m_pConnection = NULL; } } int CMyHttpClient::HttpDownload(LPCTSTR strUrl, LPCTSTR strPostData, std::vector &data) { CString strServer; CString strObject; DWORD dwServiceType; INTERNET_PORT nPort; 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_pHttpFile = m_pConnection->OpenRequest(_T("GET"), strObject, NULL, 1, NULL, NULL, (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST)); m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*"); m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn"); m_pHttpFile->AddRequestHeaders((CString)"Content-Type: application/x-www-form-urlencoded"); if (MySendHeader != "") { CString temp; temp.Format(_T("mysiginfo:%s"), MySendHeader); m_pHttpFile->AddRequestHeaders(temp); } m_pHttpFile->SendRequest(NULL, 0, NULL, 0); UINT nReaded = 0; UINT nTotalCount = 0; char szChars[BUFFER_SIZE]; while ((nReaded = m_pHttpFile->Read((void*)szChars, BUFFER_SIZE))>0){ int oldSize = data.size(); int newSzie = oldSize + nReaded; if (data.capacity()m_dwError; e->Delete(); DWORD dwError = GetLastError(); if (ERROR_INTERNET_TIMEOUT == dwErrorCode) { return OUTTIME; } else { return FAILURE; } } return SUCCESS; } int CMyHttpClient::SetSendHeader(CString send) { MySendHeader = send; return SUCCESS; } boost::shared_ptr CMyHttpClient::GetInstance() { static boost::shared_ptr instance_(new CMyHttpClient); return instance_; }