c++ 实现 http 上传和下载

news/2024/7/4 8:27:54

代码下载地址:   http://download.csdn.net/detail/mtour/8243527

 

      最近写了个程序需要用到http通讯,由于flash空间比较小,没有考虑 libcurl库,用c++封装了一个http类,实现了http  文件上传和下载

 

      

[cpp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>    
  2. #include <unistd.h>    
  3. #include <string.h>    
  4. #include <net/if.h>    
  5. #include <arpa/inet.h>    
  6. #include <sys/ioctl.h>    
  7. #include "HttpClient.h"  
  8.   
  9.   
  10.   
  11. int main()  
  12. {  
  13.     CHttpClient httpclient;  
  14.       
  15.     char* pResponse=new char[32*1024];  
  16.     memset(pResponse,0,32*1024);  
  17.       
  18.     int nRet=httpclient.ConnectServer("127.0.0.1", 80);  
  19.       
  20.     if (0!=nRet) {  
  21.         return -1;  
  22.     }  
  23.       
  24.     nRet=httpclient.HttpGet("/archives/user/10000025/jbox/m6cfaa74922bd00/JssConfig.xml", pResponse);  
  25.       
  26.     if (0!=nRet) {  
  27.         printf("http get failed\n");  
  28.         return -1;  
  29.     }  
  30.       
  31.     printf("------------- split line --------------  \n");  
  32.       
  33.     char* pTmp=strstr(pResponse, "\r\n\r\n");  
  34.     pTmp+=4;  
  35.       
  36.     printf("%s\n",pTmp);  
  37.   
  38.       
  39.     char url[128]="/file/";   
  40.       
  41.     httpclient.HttpPostFile(url, "yourfile");  
  42.       
  43.     getchar();  
  44.     //delete [] pResponse;  
  45.     return 0;  
  46. }  

 

 

类 接口定义

 

 

[cpp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. #ifndef __Design__HttpClient__  
  2. #define __Design__HttpClient__  
  3.   
  4. #include <stdio.h>  
  5. #include "net/Net.h"  
  6. #include <errno.h>  
  7. #include <netdb.h>  
  8. #include <sys/types.h>  
  9. #include <netinet/in.h>  
  10. #include <arpa/inet.h>  
  11. #include <string.h>  
  12. #include <stdlib.h>  
  13.   
  14.   
  15. class CHttpClient {  
  16. public:  
  17.     int ConnectServer(char* sHost,int nPort);  
  18.     int DisconnetServer();  
  19.     int HttpGet(char* sUrl,char* sResponse);  
  20.     int HttpPostFile(char* sUrl,char* sFileName);  
  21. private:  
  22.     char m_sHost[32];  
  23.     char m_sHostIP[32];  
  24.     int  m_nPort;  
  25.     CTcp m_tcp;  
  26. };  
  27.   
  28. #endif /* defined(__Design__HttpClient__) */  


实现

 

 

 

[cpp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
    1. //  
    2. //  HttpClient.cpp  
    3. //  Design  
    4. //  
    5. //  Created by cwliu on 14-12-5.  
    6. //  Copyright (c) 2014-12-08  cwliu. All rights reserved.  
    7. //  
    8.   
    9. #include "HttpClient.h"  
    10.   
    11. int CHttpClient::ConnectServer(char* sHost,int nPort)  
    12. {  
    13.     //gethostbyname  
    14.       
    15.     struct hostent *h;  
    16.      
    17.     if((h=gethostbyname(sHost))==NULL)  
    18.     {  
    19.         printf("gethostbyname failed\n");  
    20.         return -1;  
    21.     }  
    22.     printf("HostName :%s\n",h->h_name);  
    23.     sprintf(m_sHostIP, "%s",inet_ntoa(*((struct in_addr *)h->h_addr)));  
    24.     printf("IP Address :%s\n",m_sHostIP);  
    25.     m_nPort=nPort;  
    26.       
    27.     int nRet=m_tcp.Open(nPort, m_sHostIP);  
    28.     if (0!=nRet) {  
    29.         printf("socket connect failed\n");  
    30.         return -1;  
    31.     }  
    32.     return 0;  
    33. }  
    34. int CHttpClient::DisconnetServer()  
    35. {  
    36.     m_tcp.Close();  
    37.     return 0;  
    38. }  
    39.   
    40. int CHttpClient::HttpGet(char* sUrl,char* sResponse)  
    41. {  
    42.       
    43.     char post[300],host[100],content_len[100];  
    44.     char *lpbuf,*ptmp;  
    45.     int len=0;  
    46.     lpbuf = NULL;  
    47.     const char *header2="User-Agent: linux_http_client Http 1.0\r\nCache-Control: no-cache\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: */*\r\n";  
    48.     sprintf(post,"GET %s HTTP/1.0\r\n",sUrl);  
    49.     sprintf(host,"HOST: %s:%d\r\n",m_sHostIP,m_nPort);  
    50.     sprintf(content_len,"Content-Length: %d\r\n\r\n",1);  
    51.     len = strlen(post)+strlen(host)+strlen(header2)+strlen(content_len)+1;  
    52.     lpbuf = (char*)malloc(len);  
    53.     if(lpbuf==NULL)  
    54.     {  
    55.         return -1;  
    56.     }  
    57.     strcpy(lpbuf,post);  
    58.     strcat(lpbuf,host);  
    59.     strcat(lpbuf,header2);  
    60.     strcat(lpbuf,content_len);  
    61.     strcat(lpbuf," ");  
    62.       
    63.     m_tcp.Send((unsigned char*)lpbuf,len);  
    64.       
    65.     int nRet=m_tcp.Recv();  
    66.     if (nRet>0) {  
    67.         memcpy(sResponse,m_tcp.GetBuffer(), nRet);  
    68.         printf("%s\n",sResponse);  
    69.     }  
    70.       
    71.     return 0;  
    72. }  
    73.   
    74. int CHttpClient::HttpPostFile(char* sUrl,char* sFileName)  
    75. {  
    76.     // check file and read it  
    77.     long  nFileLen=0;  
    78.       
    79.     FILE* pFile=fopen(sFileName,"r");  
    80.     if (!pFile) {  
    81.         printf("read file failed\n");  
    82.         return -1;  
    83.     }  
    84.       
    85.     fseek(pFile, 0, SEEK_END);  
    86.     nFileLen=ftell(pFile);  
    87.     printf("File length is %ld\n", nFileLen);  
    88.       
    89.     if (!nFileLen) {  
    90.         printf("file len is 0\n");  
    91.         return -1;  
    92.     }  
    93.       
    94.     fseek(pFile, 0, SEEK_SET);  
    95.     char* sFileText=new char[nFileLen+1];  
    96.     memset(sFileText, 0, nFileLen+1);  
    97.       
    98.     fread(sFileText, 1, nFileLen, pFile);  
    99.       
    100.     fclose(pFile);  
    101.   
    102.   
    103.     char* pBody=new char[32*1024];  
    104.     memset(pBody,0,32*1024);  
    105.   
    106.     char sContentDisp[128];  
    107.     sprintf(sContentDisp, "Content-Disposition: form-data;name=\"file\";filename=\"%s\"\r\n",sFileName);  
    108.     strcat(pBody,"-------------------------------7db372eb000e2\r\n");  
    109.     strcat(pBody, sContentDisp);  
    110.     strcat(pBody, "Content-Type: text/plain\r\n\r\n");      
    111.     strcat(pBody, sFileText);      
    112.     strcat(pBody, "\r\n-------------------------------7db372eb000e2--\r\n");  
    113.       
    114.       
    115.     char post[300];  
    116.     sprintf(post,"POST %s HTTP/1.0\r\n",sUrl);  
    117.     char header[1024];  
    118.     char host[64];  
    119.     sprintf(host,"HOST: %s:%d\r\n",m_sHostIP,m_nPort);  
    120.       
    121.       
    122.     char sContentLen[32];  
    123.     sprintf(sContentLen, "Content-Length: %ld\r\n",strlen(pBody)+1);  
    124.   
    125.     // read file and caluate the file  
    126.       
    127.       
    128.     sprintf(header, "%s","Accept:*/*\r\n");      
    129.     strcat(header, host);      
    130.     strcat(header, "User-Agent: JboxHttpClient\r\n");  
    131.     strcat(header, sContentLen);  
    132.     strcat(header,"Expect: 100-continue\r\n");  
    133.     strcat(header, "Content-Type:multipart/form-data;boundary=-----------------------------7db372eb000e2\r\n\r\n");  
    134.       
    135.     char* pBuf=new char[64*1024];  
    136.     memset(pBuf, 0, 64*1024);  
    137.     strcat(pBuf, post);  
    138.     strcat(pBuf, header);   
    139.     strcat(pBuf,pBody);  
    140.       
    141.       
    142.     printf("%s\n",pBuf);  
    143.       
    144.     m_tcp.Send((unsigned char*)pBuf, strlen(pBuf)+1);  
    145.       
    146.     char sResponse[1024];  
    147.     memset(sResponse, 0, 1024);  
    148.       
    149.     int nRet=m_tcp.Recv();  
    150.     if (nRet>0) {  
    151.         memcpy(sResponse, m_tcp.GetBuffer(), nRet);  
    152.         printf("%s\n",sResponse);  
    153.   
    154.         if (strstr(sResponse,"200"))  
    155.         {  
    156.             delete[] pBody;  
    157.             delete[] pBuf;  
    158.             printf("post file success \n");  
    159.             return 0;  
    160.         }  
    161.         else  
    162.         {  
    163.             printf("post file failed \n");  
    164.         }         
    165.     }     
    166.     delete[] pBody;  
    167.     delete[] pBuf;  
    168.     return -1;  
    169. }  

http://www.niftyadmin.cn/n/2013579.html

相关文章

NodeJS 服务器基本模板

基本server配置 cookie / session / get数据 / post数据 / 请求方法 const expressrequire(express); const staticrequire(express-static); const cookieParserrequire(cookie-parser); const cookieSessionrequire(cookie-session); const bodyParserrequire(body-parser); …

mysql 中的信息数据库以及 shell操作sql

2019独角兽企业重金招聘Python工程师标准>>> Information_schema 是 MySQL 自带的信息数据库&#xff0c;里面的“表”保存着服务器当前的实时信息。它提供了访问数据库元数据的方式。 什么是元数据呢&#xff1f;元数据是关于数据的数据&#xff0c;如数据库名或表…

Makefile初探

选择一个目录创建一个Makefile文件&#xff1b; 注意第二行的开头需要时TAB建空开&#xff0c;不要用空格 执行make make的时候&#xff0c;无论你创建的是makefile还是Makefile都可以识别 &#xff0c;不在乎开头的字母是否大写。 makefile中的第一个重要概念是目标&#xff0…

葵花宝典之玩转众包——发包方

为什么80%的码农都做不了架构师&#xff1f;>>> 法则一&#xff1a; 项目发布 1. 一个简单明了的项目标题&#xff0c;有助于提高项目的报名率 项目标题是给接包方的第一印象&#xff0c;如同项目的名片&#xff0c;标题应直接明了&#xff0c;如&#xff1a;“b…

python分析nmon并获取性能指标数据的姿势是这样的

一 背景在性能测试或Linux服务器运维中&#xff0c;都会涉及对系统资源使用情况的监控&#xff0c;除了常用的系统命令外&#xff08;如top、iostat、free等&#xff09;&#xff0c;比较全面的资源数据监控工具是NMON&#xff0c;通过在服务器上运行NMON可以定期监控硬件资源并…

所有 HTTP 状态代码及其定义

代码 指示 2xx 成功 200 正常&#xff1b;请求已完成。 201 正常&#xff1b;紧接 POST 命令。 202 正常&#xff1b;已接受用于处理&#xff0c;但处理尚未完成。 203 正常&#xff1b;部分信息 — 返回的信息只是一部分。 204 正常&#xff1b;无响应 — 已接收…

Integer值判断是否相等问题

Integer值判断是否相等问题 昨天在开发中遇到一个问题&#xff0c;定义了两个Integer变量&#xff0c;暂且定义为Integer a; Integer b; 这两个值由前端赋值并传到后台&#xff0c;前台传的是a 12345, b 12345, 但我在后台比较的时候 if (a b)&#xff0c;却返回false&…

【转】Java面试题全集(上)

准备从C#转java&#xff0c;在找工作之前准备看看面试题&#xff0c;有幸看到大神的作品&#xff0c;mark一下&#xff0c;以后慢慢看。。。 2013年年底的时候&#xff0c;我看到了网上流传的一个叫做《Java面试题大全》的东西&#xff0c;认真的阅读了以后发现里面的很多题目是…