HttpClient.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // HttpClient.cpp: implementation of the CHttpClient class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "HttpClient.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CHttpClient::CHttpClient()
  15. {
  16. }
  17. CHttpClient::~CHttpClient()
  18. {
  19. }
  20. CString CHttpClient::doGet(CString href)
  21. {
  22. CString httpsource="";
  23. CInternetSession session1(NULL,0);
  24. CHttpFile* pHTTPFile=NULL;
  25. try
  26. {
  27. //pHTTPFile=(CHttpFile*)session1.OpenURL(href);
  28. pHTTPFile=(CHttpFile*)session1.OpenURL(href, 1, INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD);
  29. //session1.
  30. }
  31. catch(CInternetException)
  32. {
  33. pHTTPFile=NULL;
  34. }
  35. if(pHTTPFile)
  36. {
  37. CString text;
  38. for(int i=0;pHTTPFile->ReadString(text);i++)
  39. {
  40. httpsource=httpsource+text+"\r\n";
  41. }
  42. pHTTPFile->Close();
  43. delete pHTTPFile;
  44. }else
  45. {
  46. }
  47. return httpsource;
  48. }
  49. CString CHttpClient::doPost(CString href)
  50. {
  51. CString httpsource="";
  52. CInternetSession session1;
  53. CHttpConnection* conn1=NULL;
  54. CHttpFile* pFile = NULL;
  55. CString strServerName;
  56. CString strObject;
  57. INTERNET_PORT nPort;
  58. DWORD dwServiceType;
  59. AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort);
  60. DWORD retcode;
  61. char* outBuff = CONTENT.GetBuffer(1000);
  62. try
  63. {
  64. conn1 = session1.GetHttpConnection(strServerName,nPort);
  65. pFile = conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
  66. pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
  67. pFile -> AddRequestHeaders("Accept: */*");
  68. pFile -> SendRequest(NULL,0,outBuff,strlen(outBuff)+1);
  69. pFile -> QueryInfoStatusCode(retcode);
  70. }
  71. catch (CInternetException * e){};
  72. if(pFile)
  73. {
  74. CString text;
  75. for(int i=0;pFile->ReadString(text);i++)
  76. {
  77. httpsource=httpsource+text+"\r\n";
  78. }
  79. pFile->Close();
  80. }else
  81. {
  82. }
  83. return httpsource;
  84. delete pFile;
  85. delete conn1;
  86. session1.Close();
  87. }
  88. void CHttpClient::addParam(CString name, CString value)
  89. {
  90. names.AddTail((LPCTSTR)name);
  91. values.AddTail((LPCTSTR)value);
  92. CString eq="=";
  93. CString an="&";
  94. CONTENT=CONTENT+name+eq+value+an;
  95. CL=CONTENT.GetLength();
  96. }