#include "StdAfx.h" #include "HttpClient.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 CHttpClient::CHttpClient(LPCTSTR strAgent) : MySendHeader(_T("")) { m_pSession = new CInternetSession(strAgent); m_pConnection = NULL; m_pHttpFile = NULL; } CHttpClient::~CHttpClient(void) { Clear(); if(NULL != m_pSession) { m_pSession->Close(); delete m_pSession; m_pSession = NULL; } } void CHttpClient::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; } } string UnicodeToUtf8(const wchar_t* unicode) { string str; int len; len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL); if (len <= 0) return str; char *szUtf8 = (char*)calloc(len + 1, sizeof(char)); WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL); szUtf8[len] = '\0'; str = szUtf8; if (szUtf8) free(szUtf8); return str; } 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_pHttpFile = m_pConnection->OpenRequest(strMethod, 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 = _T("Authorization:Basic ZnhiLXNoOmVDbGo0TlRoSHlMY05USE0wZWl3ZWpVUExCcGFlZQ=="); //temp.Format(_T("fxb-sh:%s"), MySendHeader); m_pHttpFile->AddRequestHeaders(temp); } //m_pHttpFile->AddRequestHeaders((CString)"Accept-Encoding: gzip, deflate"); string end_boundary_char; int data_len=0; if( strPostData != NULL){ end_boundary_char = UnicodeToUtf8(strPostData); data_len = end_boundary_char.length(); } // m_pHttpFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData)); m_pHttpFile->SendRequest(NULL, 0, strPostData == NULL ? NULL:(char*)end_boundary_char.c_str(),strPostData == NULL ? 0:data_len); char szChars[BUFFER_SIZE + 1] = {0}; string strRawResponse = ""; UINT nReaded = 0; while ((nReaded = m_pHttpFile->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 = cs; strResponse = strRawResponse; */ DWORD dwStatusCode; BOOL success =m_pHttpFile->QueryInfoStatusCode(dwStatusCode); int len_w =MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0); WCHAR* str_w = new WCHAR[len_w]; MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,(WCHAR*)str_w,len_w); int len_a =WideCharToMultiByte(CP_ACP,0,str_w,-1,NULL,0,NULL,NULL); char* str_a=new char[len_a]; WideCharToMultiByte(CP_ACP,0,str_w,-1,str_a,len_a,NULL,NULL); strResponse =str_a; if(strResponse.size()<100&&strResponse=="{\"session\":false}"){ HWND hwnd; if(g_ShardMainWinHandle.GetWndHandle(hwnd)){ ::PostMessage(hwnd,WM_USER + 200 ,0,0); } } delete str_w; delete str_a; Clear(); if(!success||dwStatusCode<200||dwStatusCode>=300) return FAILURE; } 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 CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse) { return ExecuteRequest((LPCTSTR)_T("GET"), strUrl, strPostData, strResponse); } int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse) { return ExecuteRequest((LPCTSTR)_T("POST"), strUrl, strPostData, strResponse); } int CHttpClient::HttpPostFile( LPCTSTR strUrl,std::vector& inputs,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; } vector ff; vector ff_len; try { CString boundary_value =_T("---------------------------7b4a6d158c9"); CString boundary =_T("--")+boundary_value; CString end_boundary =boundary+_T("--"); CString end_filed =_T("--"); DWORD dwTotalRequestLength =0; char end_boundary_char[128]; int end_boundary_char_len=WideCharToMultiByte(CP_UTF8,0,end_boundary,-1,NULL,0,NULL,NULL); WideCharToMultiByte(CP_UTF8,0,end_boundary,-1,end_boundary_char,end_boundary_char_len,NULL,NULL); dwTotalRequestLength+=(end_boundary_char_len-1); for (int i=0;iGetType()==INPUT_FILED_VALUE){ field.Format(_T("%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n"),boundary,inputs[i]->GetFiledName(),inputs[i]->GetFiledValue()); filedLength=WideCharToMultiByte(CP_UTF8,0,field,-1,NULL,0,NULL,NULL); filed_str = new char[filedLength]; WideCharToMultiByte(CP_UTF8,0,field,-1,filed_str,filedLength,NULL,NULL); dwTotalRequestLength+=(filedLength-1); }else{ field.Format(_T("%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n"),boundary,inputs[i]->GetFiledName(),inputs[i]->GetFiledValue()); filedLength =WideCharToMultiByte(CP_UTF8,0,field,-1,NULL,0,NULL,NULL); filed_str = new char[filedLength]; WideCharToMultiByte(CP_UTF8,0,field,-1,filed_str,filedLength,NULL,NULL); dwTotalRequestLength+=(filedLength-1); dwTotalRequestLength+=inputs[i]->DataSize(); dwTotalRequestLength+=2; } ff.push_back(filed_str); ff_len.push_back(filedLength); } m_pConnection = m_pSession->GetHttpConnection(strServer,dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,nPort); m_pHttpFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject); m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*"); m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn"); //m_pHttpFile->AddRequestHeaders((CString)"Accept-Encoding: gzip, deflate"); m_pHttpFile->AddRequestHeaders((CString)"Content-Type: multipart/form-data; boundary="+boundary_value); if (MySendHeader != "") { CString temp; temp.Format(_T("mysiginfo:%s"), MySendHeader); m_pHttpFile->AddRequestHeaders(temp); } m_pHttpFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE); for (int i=0;iGetType()==INPUT_FILED_VALUE){ m_pHttpFile->Write(ff[i],ff_len[i]-1); }else{ m_pHttpFile->Write(ff[i],ff_len[i]-1); CInputFiled* input = inputs[i]; const int buffer_size =4*1024; unsigned char buffer[buffer_size]; int len; input->Open(); while((len =input->Read(buffer,buffer_size))>0){ m_pHttpFile->Write(buffer,len); } input->Close(); m_pHttpFile->Write("\r\n",2); } } m_pHttpFile->Write(end_boundary_char,end_boundary_char_len-1); m_pHttpFile->EndRequest(HSR_SYNC); for (int i=0;iRead((void*)szChars, BUFFER_SIZE)) > 0) { szChars[nReaded] = '\0'; strRawResponse += szChars; memset(szChars, 0, BUFFER_SIZE + 1); } int len_w =MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0); WCHAR* str_w = new WCHAR[len_w]; MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,(WCHAR*)str_w,len_w); int len_a =WideCharToMultiByte(CP_ACP,0,str_w,-1,NULL,0,NULL,NULL); char* str_a=new char[len_a]; WideCharToMultiByte(CP_ACP,0,str_w,-1,str_a,len_a,NULL,NULL); strResponse =str_a; delete str_w; delete str_a; Clear(); } catch (CInternetException* e) { for (int i=0;im_dwError; e->Delete(); DWORD dwError = GetLastError(); if (ERROR_INTERNET_TIMEOUT == dwErrorCode) { return OUTTIME; } else { return FAILURE; } } return SUCCESS; } int CHttpClient::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 CHttpClient::SetSendHeader(CString send) { MySendHeader = send; return SUCCESS; }