VC实现HTTP协议的GET和POST方法

以下是两个文件:HttpClient.h   HttpClient.cpp   一个类   
  (错误检测部分没有加入)   
  使用方法   
  1:get方式   
  CHttpClient   conn;   
  CString   value="http://8crystal.com/test.asp";   
                    value=conn.doGet(value);   
  2:post方式   
  CHttpClient   conn;   
  CString   value="http://8crystal.com/test.asp";   
  conn.addParam("name1","value1");   
  conn.addParam("name2","value2");   
  conn.addParam("name3","value3");   
  conn.addParam("name4","value4");   
  conn.addParam("name5","value5");   
  conn.addParam("name6","value6");   
  value=conn.doPost(value);         

View Code
  1 /   HttpClient.h:   interface   for   the   CHttpClient   class.   
  2   //   
  3   //////////////////////////////////////////////////////////////////////   
  4     
  5   #if   !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)   
  6   #define   AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_   
  7     
  8   #if   _MSC_VER   >   1000   
  9   #pragma   once   
 10   #endif   //   _MSC_VER   >   1000   
 11   #include   "wininet.h"   
 12   #include   "afxinet.h"   
 13   class   CHttpClient       
 14   {   
 15   public:   
 16   void   addParam(CString   name,CString   value);   
 17   CString   doPost(CString   href);   
 18   CString   doGet(CString   href);   
 19   CHttpClient();   
 20   virtual   ~CHttpClient();   
 21     
 22   private:   
 23   CString   CONTENT;   
 24   int   CL;   
 25   CStringList   values;   
 26   CStringList   names;   
 27   };   
 28     
 29   #endif   //   !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)   
 30     
 31   ++++++++++++++++++++++++++++++++++++++   
 32     
 33     
 34   //   HttpClient.cpp:   implementation   of   the   CHttpClient   class.   
 35   //   
 36   //////////////////////////////////////////////////////////////////////   
 37     
 38   #include   "stdafx.h"   
 39   #include   "emailsenderv2.h"   
 40   #include   "HttpClient.h"   
 41     
 42   #ifdef   _DEBUG   
 43   #undef   THIS_FILE   
 44   static   char   THIS_FILE[]=__FILE__;   
 45   #define   new   DEBUG_NEW   
 46   #endif   
 47     
 48   //////////////////////////////////////////////////////////////////////   
 49   //   Construction/Destruction   
 50   //////////////////////////////////////////////////////////////////////   
 51     
 52   CHttpClient::CHttpClient()   
 53   {   
 54     
 55   }   
 56     
 57   CHttpClient::~CHttpClient()   
 58   {   
 59     
 60   }   
 61     
 62   CString   CHttpClient::doGet(CString   href)   
 63   {   
 64   CString   httpsource="";   
 65   CInternetSession   session1(NULL,0);   
 66   CHttpFile*   pHTTPFile=NULL;   
 67   try{   
 68   pHTTPFile=(CHttpFile*)session1.OpenURL(href);   
 69   //session1.   
 70   }catch(CInternetException){   
 71   pHTTPFile=NULL;   
 72   }   
 73   if(pHTTPFile)   
 74   {   
 75   CString   text;   
 76   for(int   i=0;pHTTPFile->ReadString(text);i++)   
 77   {   
 78   httpsource=httpsource+text+"/r/n";   
 79   }   
 80   pHTTPFile->Close();   
 81   delete   pHTTPFile;   
 82   }else   
 83   {   
 84     
 85   }   
 86   return   httpsource;   
 87   }   
 88     
 89   CString   CHttpClient::doPost(CString   href)   
 90   {   
 91   CString   httpsource="";   
 92   CInternetSession   session1;   
 93   CHttpConnection*   conn1=NULL;   
 94           CHttpFile*   pFile   =   NULL;       
 95   CString   strServerName;   
 96           CString   strObject;   
 97           INTERNET_PORT   nPort;   
 98           DWORD   dwServiceType;   
 99   AfxParseURL((LPCTSTR)href,dwServiceType,   strServerName,   strObject,   nPort);   
100           DWORD   retcode;               
101           char*   outBuff   =   CONTENT.GetBuffer(1000);   
102           try       
103           {                   
104                   conn1   =   session1.GetHttpConnection(strServerName,nPort);           
105                   pFile   =   conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);           
106                   pFile   ->   AddRequestHeaders("Content-Type:   application/x-www-form-urlencoded");           
107                   pFile   ->   AddRequestHeaders("Accept:   */*");                           
108                   pFile   ->   SendRequest(NULL,0,outBuff,strlen(outBuff)+1);                   
109                   pFile   ->   QueryInfoStatusCode(retcode);                   
110           }                             
111           catch   (CInternetException   *   e){};                   
112   if(pFile)   
113   {   
114   CString   text;   
115   for(int   i=0;pFile->ReadString(text);i++)   
116   {   
117   httpsource=httpsource+text+"/r/n";   
118   }   
119   pFile->Close();   
120   }else   
121   {   
122     
123   }   
124   return   httpsource;   
125           delete   pFile;           
126           delete   conn1;           
127           session1.Close();   
128   }   
129     
130   void   CHttpClient::addParam(CString   name,   CString   value)   
131   {   
132   names.AddTail((LPCTSTR)name);   
133   values.AddTail((LPCTSTR)value);   
134   CString   eq="=";   
135   CString   an="&";   
136   CONTENT=CONTENT+name+eq+value+an;   
137   CL=CONTENT.GetLength();   
138   }  
原文地址:https://www.cnblogs.com/FCoding/p/2560534.html