C++使用CHttpFile實現Http請求
阿新 • • 發佈:2018-12-30
C++實現http請求的程式碼,參照網上的修改了下在mfc中使用
1、HttpClient.h
//////////////////////////////////// HttpClient.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, CString &strResponse); int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse); int HttpPut(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse); private: int ExecuteRequest(int strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse); void Clear(); private: CInternetSession *m_pSession; CHttpConnection *m_pConnection; CHttpFile *m_pFile; }; #endif // HTTPCLIENT_H
2、HttpClient.cpp
////////////////////////////////// HttpClient.cpp #include "stdafx.h" #include "HttpClient.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(int strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse) { int result =FAILURE ; //WCHAR* wPostData = strPostData.GetBuffer(); 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_pFile = m_pConnection->OpenRequest(strMethod, strObject, NULL, 1, NULL, NULL, (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST)); /*設定請求相關引數*/ m_pFile->AddRequestHeaders(L"Accept: */*,application/json");//accept請求報頭域,表示客戶端接受哪些型別的資訊 m_pFile->AddRequestHeaders(L"Accept-Charset:UTF8"); m_pFile->AddRequestHeaders(L"Accept-Language: zh-cn;q=0.8,en;q=0.6,ja;q=0.4"); m_pFile->AddRequestHeaders(L"Content-Type:application/json");//content為實體報頭域,格式及編碼 //m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData)); /*請求body內容先轉為UTF-8編碼,與服務端保持一致,cword為要傳送內容*/ char*cword; //ANSI指標 if (strPostData != NULL){ DWORD num = WideCharToMultiByte(CP_UTF8, 0, strPostData, -1, NULL, 0, NULL, NULL);//計算這個UNICODE實際由幾個UTF-8字組成 cword = (char*)calloc(num, sizeof(char)); //申請空間 if (cword == NULL) //是否申請 { free(cword); } memset(cword, 0, num*sizeof(char)); //初始化 WideCharToMultiByte(CP_UTF8, 0, strPostData, -1, cword, num, NULL, NULL); printf("content長度為%d\n", strlen(cword)); m_pFile->SendRequest(NULL, 0, cword, strlen(cword));//傳送請求 } else{ m_pFile->SendRequest(NULL, 0, NULL, 0);//傳送請求 } DWORD dwRet; m_pFile->QueryInfoStatusCode(dwRet);//查詢執行狀態 printf("HTTP_STATUS_code:%d\n", dwRet); if (dwRet == HTTP_STATUS_OK){//http請求執行失敗 result = SUCCESS; } /*儲存http響應*/ 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); } /*utf8轉unicode*/ 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); strResponse = pUnicode;//最終響應結果 //TRACE(strResponse + L""); delete[]pUnicode; pUnicode = NULL; Clear(); } catch (CInternetException* e) { Clear(); DWORD dwErrorCode = e->m_dwError; e->Delete(); DWORD dwError = GetLastError(); printf("dwError = %d", dwError, 0); strResponse = L"CInternetException\n"; if (ERROR_INTERNET_TIMEOUT == dwErrorCode) { return OUTTIME; } else { return FAILURE; } } return result; } int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse) { return ExecuteRequest(CHttpConnection::HTTP_VERB_GET, strUrl, NULL, strResponse); } int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse) { return ExecuteRequest(CHttpConnection::HTTP_VERB_POST, strUrl, strPostData, strResponse); } int CHttpClient::HttpPut(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse) { return ExecuteRequest(CHttpConnection::HTTP_VERB_PUT, strUrl, strPostData, strResponse); }