CMyHttpClient.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "StdAfx.h"
  2. #include "CMyHttpClient.h"
  3. #include <vector>
  4. //#include "ShardMainWinHandle.h"
  5. //#include "yazuoLog.h"
  6. #define BUFFER_SIZE 1024
  7. #define NORMAL_CONNECT INTERNET_FLAG_KEEP_CONNECTION
  8. #define SECURE_CONNECT NORMAL_CONNECT | INTERNET_FLAG_SECURE
  9. #define NORMAL_REQUEST INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE
  10. #define SECURE_REQUEST NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
  11. CMyHttpClient::CMyHttpClient(LPCTSTR strAgent)
  12. : MySendHeader(_T(""))
  13. {
  14. m_pSession = new CInternetSession(strAgent);
  15. m_pConnection = NULL;
  16. m_pHttpFile = NULL;
  17. }
  18. CMyHttpClient::~CMyHttpClient(void)
  19. {
  20. Clear();
  21. if (NULL != m_pSession)
  22. {
  23. m_pSession->Close();
  24. delete m_pSession;
  25. m_pSession = NULL;
  26. }
  27. }
  28. void CMyHttpClient::Clear()
  29. {
  30. if (NULL != m_pHttpFile)
  31. {
  32. m_pHttpFile->Close();
  33. delete m_pHttpFile;
  34. m_pHttpFile = NULL;
  35. }
  36. if (NULL != m_pConnection)
  37. {
  38. m_pConnection->Close();
  39. delete m_pConnection;
  40. m_pConnection = NULL;
  41. }
  42. }
  43. int CMyHttpClient::HttpDownload(LPCTSTR strUrl, LPCTSTR strPostData, std::vector<char> &data)
  44. {
  45. CString strServer;
  46. CString strObject;
  47. DWORD dwServiceType;
  48. INTERNET_PORT nPort;
  49. AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
  50. if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
  51. {
  52. return FAILURE;
  53. }
  54. try
  55. {
  56. m_pConnection = m_pSession->GetHttpConnection(strServer, dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT, nPort);
  57. m_pHttpFile = m_pConnection->OpenRequest(_T("GET"), strObject, NULL, 1, NULL, NULL, (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
  58. m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*");
  59. m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn");
  60. m_pHttpFile->AddRequestHeaders((CString)"Content-Type: application/x-www-form-urlencoded");
  61. if (MySendHeader != "")
  62. {
  63. CString temp;
  64. temp.Format(_T("mysiginfo:%s"), MySendHeader);
  65. m_pHttpFile->AddRequestHeaders(temp);
  66. }
  67. m_pHttpFile->SendRequest(NULL, 0, NULL, 0);
  68. UINT nReaded = 0;
  69. UINT nTotalCount = 0;
  70. char szChars[BUFFER_SIZE];
  71. while ((nReaded = m_pHttpFile->Read((void*)szChars, BUFFER_SIZE))>0){
  72. int oldSize = data.size();
  73. int newSzie = oldSize + nReaded;
  74. if (data.capacity()<newSzie){
  75. data.reserve(data.capacity() + 1024 * 512);
  76. }
  77. data.resize(newSzie);
  78. memcpy(data.data() + oldSize, szChars, nReaded);
  79. }
  80. Clear();
  81. }
  82. catch (CInternetException* e)
  83. {
  84. Clear();
  85. DWORD dwErrorCode = e->m_dwError;
  86. e->Delete();
  87. DWORD dwError = GetLastError();
  88. if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
  89. {
  90. return OUTTIME;
  91. }
  92. else
  93. {
  94. return FAILURE;
  95. }
  96. }
  97. return SUCCESS;
  98. }
  99. int CMyHttpClient::SetSendHeader(CString send)
  100. {
  101. MySendHeader = send;
  102. return SUCCESS;
  103. }
  104. boost::shared_ptr<CMyHttpClient> CMyHttpClient::GetInstance()
  105. {
  106. static boost::shared_ptr<CMyHttpClient> instance_(new CMyHttpClient);
  107. return instance_;
  108. }