123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include "StdAfx.h"
- #include "CMyHttpClient.h"
- #include <vector>
- //#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<char> &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()<newSzie){
- data.reserve(data.capacity() + 1024 * 512);
- }
- data.resize(newSzie);
- memcpy(data.data() + oldSize, szChars, nReaded);
- }
- Clear();
- }
- catch (CInternetException* e)
- {
- Clear();
- DWORD dwErrorCode = e->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> CMyHttpClient::GetInstance()
- {
- static boost::shared_ptr<CMyHttpClient> instance_(new CMyHttpClient);
- return instance_;
- }
|