c++ 构造http post 发文件

直接看代码#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <string.h>
#include <netdb.h>
#include <cerrno>


using namespace std;


std::string get_file_contents(const char *filename)
{
  std::FILE *fp = std::fopen(filename, "rb");
  if (fp)
  {
    std::string contents;
    std::fseek(fp, 0, SEEK_END);
    contents.resize(std::ftell(fp));
    std::rewind(fp);
    std::fread(&contents[0], 1, contents.size(), fp);
    std::fclose(fp);
    return(contents);
  }
  throw(errno);
}


int request(char* hostname, char* api,string & content)
{


    //鲁玫炉socket
    struct hostent* host_addr = gethostbyname(hostname);
    if (host_addr == NULL)
    {
        cout<<"Unable to locate host"<<endl;
        return -103;
    }




    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons((unsigned short)80);
    sin.sin_addr=  *((struct in_addr *)host_addr->h_addr);
    bzero(&(sin.sin_zero),8);


    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1)
    {
        return -100;
    }




    if (connect(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr) ) == -1)
    {
        cout<<"connect failed"<<endl;
        return -101;
    }




    //鲁玫炉路垄脣脨息
    char send_str[2048] = {0};




    //头脨息
    strcat(send_str, "POST ");
    strcat(send_str, api);
    strcat(send_str, " HTTP/1.1\r\n");
    strcat(send_str, "Host: ");
    strcat(send_str, hostname);
    strcat(send_str, "\r\n");
    //strcat(send_str, "Connection: keep-alive\r\n");
    char endsss[190] = {0};
    strcat(endsss,"------WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
    strcat(endsss,"Content-Disposition: form-data; name=\"file\"; filename=\"/home/heida/test/1152_648.png\"\r\n");
    strcat(endsss,"Content-Type: application/octet-stream\r\n\r\n");


    char ends[190] = {0};
    strcat(ends, "\r\n------WebKitFormBoundarybZRmyZBq9p09AotO--\r\n");


    char content_header[100];
    sprintf(content_header,"Content-Length: %d\r\n",content.size()+strlen(endsss)+strlen(ends));




    strcat(send_str, content_header);
    strcat(send_str, "Cache-Control: no-cache\r\n");
   // strcat(send_str, "Origin: http://yyfs.yy.com\r\n");
  //  strcat(send_str, "Upgrade-Insecure-Requests: 1\r\n");
   // strcat(send_str, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1\r\n");
    strcat(send_str, "Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
    strcat(send_str, "Accept: */*\r\n");
    //strcat(send_str, "Referer: http://yyfs.yy.com/\r\n");
    strcat(send_str, "Accept-Encoding: gzip,deflate,sdch\r\n");
    strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8\r\n");
    //strcat(send_str,"Expect: 100-continue\r\n");    


    //脛脠脨息
    strcat(send_str, "\r\n\r\n");
//    strcat(send_str,"------WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
  //  strcat(send_str,"Content-Disposition: form-data; name=\"type\"\r\n\r\n");
   // strcat(send_str, "\r\n");
//    strcat(send_str, "HTTP/1.1 100 Continue\r\n");
  //  strcat(send_str,"Content-Transfer-Encoding: binary\r\n");
    cout<<send_str<<endl;    
    cout<<endsss<<endl;
    if (write(sock, send_str, strlen(send_str)) == -1)
    {
        cout<<"send failed"<<endl;
        return -101;
    }
    if (write(sock, endsss, strlen(endsss)) == -1)
     {
        cout<<"send failed"<<endl;
        return -101;


     }
    cout<<content.c_str()<<endl;
    if (write(sock, content.c_str(), content.size()) == -1)
    {


       cout<<"send failed"<<endl;
       return -1;
    }
    cout<<ends<<endl;
    if (write(sock, ends, strlen(ends)) == -1)
    {
      cout<<"send failed"<<endl;
      return -1;


    }




    //禄帽碌禄脴脜垄
    char recv_str[4096] = {0};
    if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)
    {
        cout<<"recv failed"<<endl;
        return -101;
    }




    cout<<recv_str<<endl;


    return 0;
}


int main()
{
    std::string files =  get_file_contents("1152_648.png");
    cout<<files.size()<<endl;
    request("yswdd.com","http://yujds.com/FileUploadDownload/uploadv2_t.php?cookie=ss",files);






    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值