加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

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

杂谈

分类: 计算机与 Internet

依赖于前面的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

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有