HttpClient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "StdAfx.h"
  2. #include "HttpClient.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. CHttpClient::CHttpClient(LPCTSTR strAgent)
  12. : MySendHeader(_T(""))
  13. {
  14. m_pSession = new CInternetSession(strAgent);
  15. m_pConnection = NULL;
  16. m_pHttpFile = NULL;
  17. }
  18. CHttpClient::~CHttpClient(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 CHttpClient::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. string UnicodeToUtf8(const wchar_t* unicode)
  44. {
  45. string str;
  46. int len;
  47. len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
  48. if (len <= 0) return str;
  49. char *szUtf8 = (char*)calloc(len + 1, sizeof(char));
  50. WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
  51. szUtf8[len] = '\0';
  52. str = szUtf8;
  53. if (szUtf8) free(szUtf8);
  54. return str;
  55. }
  56. int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
  57. {
  58. CString strServer;
  59. CString strObject;
  60. DWORD dwServiceType;
  61. INTERNET_PORT nPort;
  62. strResponse = "";
  63. AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
  64. if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
  65. {
  66. return FAILURE;
  67. }
  68. try
  69. {
  70. m_pConnection = m_pSession->GetHttpConnection(strServer,dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT, nPort);
  71. m_pHttpFile = m_pConnection->OpenRequest(strMethod, strObject, NULL, 1, NULL, NULL, (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
  72. m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*");
  73. m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn");
  74. m_pHttpFile->AddRequestHeaders((CString)"Content-Type: application/x-www-form-urlencoded");
  75. //if (MySendHeader != "")
  76. {
  77. CString temp = _T("Authorization:Basic ZnhiLXNoOmVDbGo0TlRoSHlMY05USE0wZWl3ZWpVUExCcGFlZQ==");
  78. //temp.Format(_T("fxb-sh:%s"), MySendHeader);
  79. m_pHttpFile->AddRequestHeaders(temp);
  80. }
  81. //m_pHttpFile->AddRequestHeaders((CString)"Accept-Encoding: gzip, deflate");
  82. string end_boundary_char;
  83. int data_len=0;
  84. if( strPostData != NULL){
  85. end_boundary_char = UnicodeToUtf8(strPostData);
  86. data_len = end_boundary_char.length();
  87. }
  88. // m_pHttpFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
  89. m_pHttpFile->SendRequest(NULL, 0, strPostData == NULL ? NULL:(char*)end_boundary_char.c_str(),strPostData == NULL ? 0:data_len);
  90. char szChars[BUFFER_SIZE + 1] = {0};
  91. string strRawResponse = "";
  92. UINT nReaded = 0;
  93. while ((nReaded = m_pHttpFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
  94. {
  95. szChars[nReaded] = '\0';
  96. strRawResponse += szChars;
  97. memset(szChars, 0, BUFFER_SIZE + 1);
  98. }
  99. /* int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
  100. WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
  101. memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
  102. MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
  103. CString cs(pUnicode);
  104. delete []pUnicode;
  105. pUnicode = NULL;
  106. strResponse = cs;
  107. strResponse = strRawResponse;
  108. */
  109. DWORD dwStatusCode;
  110. BOOL success =m_pHttpFile->QueryInfoStatusCode(dwStatusCode);
  111. int len_w =MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0);
  112. WCHAR* str_w = new WCHAR[len_w];
  113. MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,(WCHAR*)str_w,len_w);
  114. int len_a =WideCharToMultiByte(CP_ACP,0,str_w,-1,NULL,0,NULL,NULL);
  115. char* str_a=new char[len_a];
  116. WideCharToMultiByte(CP_ACP,0,str_w,-1,str_a,len_a,NULL,NULL);
  117. strResponse =str_a;
  118. if(strResponse.size()<100&&strResponse=="{\"session\":false}"){
  119. HWND hwnd;
  120. if(g_ShardMainWinHandle.GetWndHandle(hwnd)){
  121. ::PostMessage(hwnd,WM_USER + 200 ,0,0);
  122. }
  123. }
  124. delete str_w;
  125. delete str_a;
  126. Clear();
  127. if(!success||dwStatusCode<200||dwStatusCode>=300) return FAILURE;
  128. }
  129. catch (CInternetException* e)
  130. {
  131. Clear();
  132. DWORD dwErrorCode = e->m_dwError;
  133. e->Delete();
  134. DWORD dwError = GetLastError();
  135. if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
  136. {
  137. return OUTTIME;
  138. }
  139. else
  140. {
  141. return FAILURE;
  142. }
  143. }
  144. return SUCCESS;
  145. }
  146. int CHttpClient::HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
  147. {
  148. return ExecuteRequest((LPCTSTR)_T("GET"), strUrl, strPostData, strResponse);
  149. }
  150. int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
  151. {
  152. return ExecuteRequest((LPCTSTR)_T("POST"), strUrl, strPostData, strResponse);
  153. }
  154. int CHttpClient::HttpPostFile( LPCTSTR strUrl,std::vector<CInputFiled*>& inputs,string &strResponse )
  155. {
  156. CString strServer;
  157. CString strObject;
  158. DWORD dwServiceType;
  159. INTERNET_PORT nPort;
  160. strResponse = "";
  161. AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
  162. if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
  163. {
  164. return FAILURE;
  165. }
  166. vector<char*> ff;
  167. vector<int> ff_len;
  168. try
  169. {
  170. CString boundary_value =_T("---------------------------7b4a6d158c9");
  171. CString boundary =_T("--")+boundary_value;
  172. CString end_boundary =boundary+_T("--");
  173. CString end_filed =_T("--");
  174. DWORD dwTotalRequestLength =0;
  175. char end_boundary_char[128];
  176. int end_boundary_char_len=WideCharToMultiByte(CP_UTF8,0,end_boundary,-1,NULL,0,NULL,NULL);
  177. WideCharToMultiByte(CP_UTF8,0,end_boundary,-1,end_boundary_char,end_boundary_char_len,NULL,NULL);
  178. dwTotalRequestLength+=(end_boundary_char_len-1);
  179. for (int i=0;i<inputs.size();i++)
  180. {
  181. CString field;
  182. char * filed_str =NULL;
  183. int filedLength =0;
  184. if(inputs[i]->GetType()==INPUT_FILED_VALUE){
  185. 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());
  186. filedLength=WideCharToMultiByte(CP_UTF8,0,field,-1,NULL,0,NULL,NULL);
  187. filed_str = new char[filedLength];
  188. WideCharToMultiByte(CP_UTF8,0,field,-1,filed_str,filedLength,NULL,NULL);
  189. dwTotalRequestLength+=(filedLength-1);
  190. }else{
  191. 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());
  192. filedLength =WideCharToMultiByte(CP_UTF8,0,field,-1,NULL,0,NULL,NULL);
  193. filed_str = new char[filedLength];
  194. WideCharToMultiByte(CP_UTF8,0,field,-1,filed_str,filedLength,NULL,NULL);
  195. dwTotalRequestLength+=(filedLength-1);
  196. dwTotalRequestLength+=inputs[i]->DataSize();
  197. dwTotalRequestLength+=2;
  198. }
  199. ff.push_back(filed_str);
  200. ff_len.push_back(filedLength);
  201. }
  202. m_pConnection = m_pSession->GetHttpConnection(strServer,dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,nPort);
  203. m_pHttpFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
  204. m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*");
  205. m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn");
  206. //m_pHttpFile->AddRequestHeaders((CString)"Accept-Encoding: gzip, deflate");
  207. m_pHttpFile->AddRequestHeaders((CString)"Content-Type: multipart/form-data; boundary="+boundary_value);
  208. if (MySendHeader != "")
  209. {
  210. CString temp;
  211. temp.Format(_T("mysiginfo:%s"), MySendHeader);
  212. m_pHttpFile->AddRequestHeaders(temp);
  213. }
  214. m_pHttpFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
  215. for (int i=0;i<inputs.size();i++)
  216. {
  217. if(inputs[i]->GetType()==INPUT_FILED_VALUE){
  218. m_pHttpFile->Write(ff[i],ff_len[i]-1);
  219. }else{
  220. m_pHttpFile->Write(ff[i],ff_len[i]-1);
  221. CInputFiled* input = inputs[i];
  222. const int buffer_size =4*1024;
  223. unsigned char buffer[buffer_size];
  224. int len;
  225. input->Open();
  226. while((len =input->Read(buffer,buffer_size))>0){
  227. m_pHttpFile->Write(buffer,len);
  228. }
  229. input->Close();
  230. m_pHttpFile->Write("\r\n",2);
  231. }
  232. }
  233. m_pHttpFile->Write(end_boundary_char,end_boundary_char_len-1);
  234. m_pHttpFile->EndRequest(HSR_SYNC);
  235. for (int i=0;i<ff.size();i++)
  236. {
  237. delete ff[i];
  238. }
  239. char szChars[BUFFER_SIZE + 1] = {0};
  240. string strRawResponse = "";
  241. UINT nReaded = 0;
  242. while ((nReaded = m_pHttpFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
  243. {
  244. szChars[nReaded] = '\0';
  245. strRawResponse += szChars;
  246. memset(szChars, 0, BUFFER_SIZE + 1);
  247. }
  248. int len_w =MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0);
  249. WCHAR* str_w = new WCHAR[len_w];
  250. MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,(WCHAR*)str_w,len_w);
  251. int len_a =WideCharToMultiByte(CP_ACP,0,str_w,-1,NULL,0,NULL,NULL);
  252. char* str_a=new char[len_a];
  253. WideCharToMultiByte(CP_ACP,0,str_w,-1,str_a,len_a,NULL,NULL);
  254. strResponse =str_a;
  255. delete str_w;
  256. delete str_a;
  257. Clear();
  258. }
  259. catch (CInternetException* e)
  260. {
  261. for (int i=0;i<ff.size();i++)
  262. {
  263. delete ff[i];
  264. }
  265. Clear();
  266. DWORD dwErrorCode = e->m_dwError;
  267. e->Delete();
  268. DWORD dwError = GetLastError();
  269. if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
  270. {
  271. return OUTTIME;
  272. }
  273. else
  274. {
  275. return FAILURE;
  276. }
  277. }
  278. return SUCCESS;
  279. }
  280. int CHttpClient::HttpDownload( LPCTSTR strUrl, LPCTSTR strPostData,std::vector<char> &data)
  281. {
  282. CString strServer;
  283. CString strObject;
  284. DWORD dwServiceType;
  285. INTERNET_PORT nPort;
  286. AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
  287. if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
  288. {
  289. return FAILURE;
  290. }
  291. try
  292. {
  293. m_pConnection = m_pSession->GetHttpConnection(strServer,dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT, nPort);
  294. m_pHttpFile = m_pConnection->OpenRequest(_T("GET"), strObject, NULL, 1, NULL, NULL, (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
  295. m_pHttpFile->AddRequestHeaders((CString)"Accept: *,*/*");
  296. m_pHttpFile->AddRequestHeaders((CString)"Accept-Language: zh-cn");
  297. m_pHttpFile->AddRequestHeaders((CString)"Content-Type: application/x-www-form-urlencoded");
  298. if (MySendHeader != "")
  299. {
  300. CString temp;
  301. temp.Format(_T("mysiginfo:%s"), MySendHeader);
  302. m_pHttpFile->AddRequestHeaders(temp);
  303. }
  304. m_pHttpFile->SendRequest(NULL, 0, NULL,0);
  305. UINT nReaded = 0;
  306. UINT nTotalCount=0;
  307. char szChars[BUFFER_SIZE];
  308. while ((nReaded = m_pHttpFile->Read((void*)szChars, BUFFER_SIZE))>0){
  309. int oldSize =data.size();
  310. int newSzie=oldSize+nReaded;
  311. if(data.capacity()<newSzie){
  312. data.reserve(data.capacity()+1024*512);
  313. }
  314. data.resize(newSzie);
  315. memcpy(data.data()+oldSize,szChars,nReaded);
  316. }
  317. Clear();
  318. }
  319. catch (CInternetException* e)
  320. {
  321. Clear();
  322. DWORD dwErrorCode = e->m_dwError;
  323. e->Delete();
  324. DWORD dwError = GetLastError();
  325. if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
  326. {
  327. return OUTTIME;
  328. }
  329. else
  330. {
  331. return FAILURE;
  332. }
  333. }
  334. return SUCCESS;
  335. }
  336. int CHttpClient::SetSendHeader(CString send)
  337. {
  338. MySendHeader = send;
  339. return SUCCESS;
  340. }