C++使用CHttpFile实现Http请求

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 09:08   36   0

C++实现http请求的代码,参照网上的修改了下在mfc中使用

1、HttpClient.h

  1. ////////////////////////////////////HttpClient.h
  2. #ifndefHTTPCLIENT_H
  3. #defineHTTPCLIENT_H
  4. #include<afxinet.h>
  5. #include<string>
  6. usingnamespacestd;
  7. #defineIE_AGENT_T("Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;.NETCLR2.0.50727)")
  8. //操作成功
  9. #defineSUCCESS0
  10. //操作失败
  11. #defineFAILURE1
  12. //操作超时www.it165.net
  13. #defineOUTTIME2
  14. classCHttpClient
  15. {
  16. public:
  17. CHttpClient(LPCTSTRstrAgent=IE_AGENT);
  18. virtual~CHttpClient(void);
  19. intHttpGet(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
  20. intHttpPost(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
  21. intHttpPut(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
  22. private:
  23. intExecuteRequest(intstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
  24. voidClear();
  25. private:
  26. CInternetSession*m_pSession;
  27. CHttpConnection*m_pConnection;
  28. CHttpFile*m_pFile;
  29. };
  30. #endif//HTTPCLIENT_H

2、HttpClient.cpp

  1. //////////////////////////////////HttpClient.cpp
  2. #include"stdafx.h"
  3. #include"HttpClient.h"
  4. #defineBUFFER_SIZE1024
  5. #defineNORMAL_CONNECTINTERNET_FLAG_KEEP_CONNECTION
  6. #defineSECURE_CONNECTNORMAL_CONNECT|INTERNET_FLAG_SECURE
  7. #defineNORMAL_REQUESTINTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE
  8. #defineSECURE_REQUESTNORMAL_REQUEST|INTERNET_FLAG_SECURE|INTERNET_FLAG_IGNORE_CERT_CN_INVALID
  9. CHttpClient::CHttpClient(LPCTSTRstrAgent)
  10. {
  11. m_pSession=newCInternetSession(strAgent);
  12. m_pConnection=NULL;
  13. m_pFile=NULL;
  14. }
  15. CHttpClient::~CHttpClient(void)
  16. {
  17. Clear();
  18. if(NULL!=m_pSession)
  19. {
  20. m_pSession->Close();
  21. deletem_pSession;
  22. m_pSession=NULL;
  23. }
  24. }
  25. voidCHttpClient::Clear()
  26. {
  27. if(NULL!=m_pFile)
  28. {
  29. m_pFile->Close();
  30. deletem_pFile;
  31. m_pFile=NULL;
  32. }
  33. if(NULL!=m_pConnection)
  34. {
  35. m_pConnection->Close();
  36. deletem_pConnection;
  37. m_pConnection=NULL;
  38. }
  39. }
  40. intCHttpClient::ExecuteRequest(intstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
  41. {
  42. intresult=FAILURE;
  43. //WCHAR*wPostData=strPostData.GetBuffer();
  44. CStringstrServer;
  45. CStringstrObject;
  46. DWORDdwServiceType;
  47. INTERNET_PORTnPort;
  48. AfxParseURL(strUrl,dwServiceType,strServer,strObject,nPort);
  49. if(AFX_INET_SERVICE_HTTP!=dwServiceType&&AFX_INET_SERVICE_HTTPS!=dwServiceType)
  50. {
  51. returnFAILURE;
  52. }
  53. try
  54. {
  55. m_pConnection=m_pSession->GetHttpConnection(strServer,
  56. dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_CONNECT:SECURE_CONNECT,
  57. nPort);
  58. m_pFile=m_pConnection->OpenRequest(strMethod,strObject,
  59. NULL,1,NULL,NULL,
  60. (dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_REQUEST:SECURE_REQUEST));
  61. /*设置请求相关参数*/
  62. m_pFile->AddRequestHeaders(L"Accept:*/*,application/json");//accept请求报头域,表示客户端接受哪些类型的信息
  63. m_pFile->AddRequestHeaders(L"Accept-Charset:UTF8");
  64. m_pFile->AddRequestHeaders(L"Accept-Language:zh-cn;q=0.8,en;q=0.6,ja;q=0.4");
  65. m_pFile->AddRequestHeaders(L"Content-Type:application/json");//content为实体报头域,格式及编码
  66. //m_pFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPostData,strPostData==NULL?0:_tcslen(strPostData));
  67. /*请求body内容先转为UTF-8编码,与服务端保持一致,cword为要发送内容*/
  68. char*cword;//ANSI指针
  69. if(strPostData!=NULL){
  70. DWORDnum=WideCharToMultiByte(CP_UTF8,0,strPostData,-1,NULL,0,NULL,NULL);//計算這個UNICODE实际由几个UTF-8字組成
  71. cword=(char*)calloc(num,sizeof(char));//申请空间
  72. if(cword==NULL)//是否申请
  73. {
  74. free(cword);
  75. }
  76. memset(cword,0,num*sizeof(char));//初始化
  77. WideCharToMultiByte(CP_UTF8,0,strPostData,-1,cword,num,NULL,NULL);
  78. printf("content长度为%d\n",strlen(cword));
  79. m_pFile->SendRequest(NULL,0,cword,strlen(cword));//发送请求
  80. }
  81. else{
  82. m_pFile->SendRequest(NULL,0,NULL,0);//发送请求
  83. }
  84. DWORDdwRet;
  85. m_pFile->QueryInfoStatusCode(dwRet);//查询执行状态
  86. printf("HTTP_STATUS_code:%d\n",dwRet);
  87. if(dwRet==HTTP_STATUS_OK){//http请求执行失败
  88. result=SUCCESS;
  89. }
  90. /*保存http响应*/
  91. charszChars[BUFFER_SIZE+1]={0};
  92. stringstrRawResponse="";
  93. UINTnReaded=0;
  94. while((nReaded=m_pFile->Read((void*)szChars,BUFFER_SIZE))>0)
  95. {
  96. szChars[nReaded]='\0';
  97. strRawResponse+=szChars;
  98. memset(szChars,0,BUFFER_SIZE+1);
  99. }
  100. /*utf8转unicode*/
  101. intunicodeLen=MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0);
  102. WCHAR*pUnicode=newWCHAR[unicodeLen+1];
  103. memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
  104. MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,pUnicode,unicodeLen);
  105. strResponse=pUnicode;//最终响应结果
  106. //TRACE(strResponse+L"");
  107. delete[]pUnicode;
  108. pUnicode=NULL;
  109. Clear();
  110. }
  111. catch(CInternetException*e)
  112. {
  113. Clear();
  114. DWORDdwErrorCode=e->m_dwError;
  115. e->Delete();
  116. DWORDdwError=GetLastError();
  117. printf("dwError=%d",dwError,0);
  118. strResponse=L"CInternetException\n";
  119. if(ERROR_INTERNET_TIMEOUT==dwErrorCode)
  120. {
  121. returnOUTTIME;
  122. }
  123. else
  124. {
  125. returnFAILURE;
  126. }
  127. }
  128. returnresult;
  129. }
  130. intCHttpClient::HttpGet(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
  131. {
  132. returnExecuteRequest(CHttpConnection::HTTP_VERB_GET,strUrl,NULL,strResponse);
  133. }
  134. intCHttpClient::HttpPost(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
  135. {
  136. returnExecuteRequest(CHttpConnection::HTTP_VERB_POST,strUrl,strPostData,strResponse);
  137. }
  138. intCHttpClient::HttpPut(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
  139. {
  140. returnExecuteRequest(CHttpConnection::HTTP_VERB_PUT,strUrl,strPostData,strResponse);
  141. }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP