天天看点

VC实现HTTP协议的GET和POST方法(含DEMO)

最近在做一个网络摄像机集中监控端的项目,客户需要通过机身编码获得摄像机的IP和端口,客户给了一个http的地址,我使用VC作为开发环境,之前做过socket通信方面的项目,但感觉用在这上面有点小题大作,网上找了找,可以使用wininet来实现,下面是HttpClient类文件,另附相关DEMO供大家下载参考!如果有好的实现方法,欢迎分享!

以下是两个文件:HttpClient.h   HttpClient.cpp   一个类  

  (错误检测部分没有加入)  

  使用方法  

  1:get方式  

  CHttpClient   conn;  

  CString   value="http://www.seuoutshine.com/guestbook.asp";  

                    value=conn.doGet(value);  

  2:post方式  

  CHttpClient   conn;  

  CString   value="http://www.seuoutshine.com/guestbook.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);        

  //   HttpClient.h:   interface   for   the   CHttpClient   class.  

  //  

  //  

  #if   !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)  

  #define   AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_  

  #if   _MSC_VER   >   1000  

  #pragma   once  

  #endif   //   _MSC_VER   >   1000  

  #include   "wininet.h"  

  #include   "afxinet.h"  

  class   CHttpClient      

  {  

  public:  

  void   addParam(CString   name,CString   value);  

  CString   doPost(CString   href);  

  CString   doGet(CString   href);  

  CHttpClient();  

  virtual   ~CHttpClient();  

  private:  

  CString   CONTENT;  

  int   CL;  

  CStringList   values;  

  CStringList   names;  

  };  

  #endif   //   !defined(AFX_HTTPCLIENT_H__EA769DCB_AAB9_47CD_BD87_FBD6913592C5__INCLUDED_)  

  ++++++++++++++++++++++++++++++++++++++  

  //   HttpClient.cpp:   implementation   of   the   CHttpClient   class.  

  //  

  //  

  #include   "stdafx.h"  

  #include   "emailsenderv2.h"  

  #include   "HttpClient.h"  

  #ifdef   _DEBUG  

  #undef   THIS_FILE  

  static   char   THIS_FILE[]=__FILE__;  

  #define   new   DEBUG_NEW  

  #endif  

  //  

  //   Construction/Destruction  

  //  

  CHttpClient::CHttpClient()  

  {  

  }  

  CHttpClient::~CHttpClient()  

  {  

  }  

  CString   CHttpClient::doGet(CString   href)  

  {  

  CString   httpsource="";  

  CInternetSession   session1(NULL,0);  

  CHttpFile*   pHTTPFile=NULL;  

  try{  

  pHTTPFile=(CHttpFile*)session1.OpenURL(href);  

  //session1.  

  }catch(CInternetException){  

  pHTTPFile=NULL;  

  }  

  if(pHTTPFile)  

  {  

  CString   text;  

  for(int   i=0;pHTTPFile->ReadString(text);i++)  

  {  

  httpsource=httpsource+text+"/r/n";  

  }  

  pHTTPFile->Close();  

  delete   pHTTPFile;  

  }else  

  {  

  }  

  return   httpsource;  

  }  

  CString   CHttpClient::doPost(CString   href)  

  {  

  CString   httpsource="";  

  CInternetSession   session1;  

  CHttpConnection*   conn1=NULL;  

          CHttpFile*   pFile   =   NULL;      

  CString   strServerName;  

          CString   strObject;  

          INTERNET_PORT   nPort;  

          DWORD   dwServiceType;  

  AfxParseURL((LPCTSTR)href,dwServiceType,   strServerName,   strObject,   nPort);  

          DWORD   retcode;              

          char*   outBuff   =   CONTENT.GetBuffer(1000);  

          try      

          {                  

                  conn1   =   session1.GetHttpConnection(strServerName,nPort);          

                  pFile   =   conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);         

                  pFile   ->   AddRequestHeaders("Content-Type:   application/x-www-form-urlencoded");          

                  pFile   ->   AddRequestHeaders("Accept:   */*");                          

                  pFile   ->   SendRequest(NULL,0,outBuff,strlen(outBuff)+1);                  

                  pFile   ->   QueryInfoStatusCode(retcode);                  

          }                            

          catch   (CInternetException   *   e){};                  

  if(pFile)  

  {  

  CString   text;  

  for(int   i=0;pFile->ReadString(text);i++)  

  {  

  httpsource=httpsource+text+"/r/n";  

  }  

  pFile->Close();  

  }else  

  {  

  }  

  return   httpsource;  

          delete   pFile;          

          delete   conn1;          

          session1.Close();  

  }  

  void   CHttpClient::addParam(CString   name,   CString   value)  

  {  

  names.AddTail((LPCTSTR)name);  

  values.AddTail((LPCTSTR)value);  

  CString   eq="=";  

  CString   an="&";  

  CONTENT=CONTENT+name+eq+value+an;  

  CL=CONTENT.GetLength();  

  } 

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