分享

跨平台 C++ Http Get,Post函数

 quasiceo 2012-12-13


跨平台C++ Http Get和Post 函数,http.h

可以下载网页或者提交表单


//---------------------------------------------------------------------------

#ifndef HttpUnitH
#define HttpUnitH
#include <string>
using namespace std;
//!下载一个URL并放到body中
bool HttpDownload(string URL,string &body,const string& post="");

//---------------------------------------------------------------------------
#endif

跨平台 C++ Http Get,Post函数,http.cpp

(2005-05-26 11:14:46)


依赖于前面的sock类库, CConvert::IntToStr可以单独写一个好了.

//---------------------------------------------------------------------------


#include "http.h"
#include "sock.h"
#include "convert.h"
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma package(smart_init)

bool HttpDownload(string url,string &body,const string& post)
{
      body="";
      int pos=url.find ('/',8);
      if(pos==-1)
      {
              return false;
      }
      string host=url.substr(7,pos-7);
      string remotepath=url.substr (pos,url.length ());
      pos=host.find(':');
      int port=80;
      if(pos!=-1)
      {
              port=atoi(host.substr(pos+1,host.length()).c_str());
              if(port==0)
                  port=80;
              host=host.substr(0,pos);
      }


      Sock m_sock;
      if(!m_sock.Connect(host,port))
      {
              return false;
      }

      string cmd;
   if(post=="")
   {
    cmd="GET "+remotepath+" HTTP/1.0\r\nHost: "+host+"\r\n\r\n";
   }
   else
   {
    cmd="POST "+remotepath+" HTTP/1.0\r\nHost: "+host+"\r\nContent-Length:"+CConvert::IntToStr(post.length())+"\r\n\r\n"+post;
   }
      m_sock.Send((char*)cmd.c_str(),cmd.length());

      char* buf=new char[65537];
      bool head=true;
      try
      {
              int len=0;
              do
              {
                      len=0;
                      len=m_sock.Recv(buf,65536);
                      if(len>0)
                      {
                              buf[len]='\0';
                              if(head)
                              {
                                  char* pStatue=strchr(buf,' ');
                                      char* headend=strstr(buf,"\r\n\r\n");
        if((pStatue==NULL)||(pStatue[1]!='2'))
        {
         delete [] buf;
      m_sock.Close();
      return false;
        }
       
                                      if(headend!=NULL)
                                              headend+=4;
                                      else
                                      {
                                              headend=strstr(buf,"\n\n");
                                              if(headend!=NULL)
                                                      headend+=2;
                                      }
                                      if(headend!=NULL)
                                      {
                                              head=false;
                                              body+=string(headend,buf+len-headend);
                                      }
                              }else
                                      body+=string(buf,len);
                      }
              }while(len>0);
      }catch(...){}
      delete [] buf;
      m_sock.Close();
      return true;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约