c++ 实现 http 上传和下载

简介: 代码下载地址:   http://download.csdn.net/detail/mtour/8243527         最近写了个程序需要用到http通讯,由于flash空间比较小,没有考虑 libcurl库,用c++封装了一个http类,实现了http  文件上传和下载      ...

代码下载地址:   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. }  
目录
相关文章
|
2月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
179 3
|
3月前
|
C# 图形学 开发者
Unity开发中使用UnityWebRequest从HTTP服务器下载资源。
总之,UnityWebRequest就是游戏开发者手中的万能钓鱼竿,既可以获取文本数据,也能钓上图片资源,甚至是那声音的涟漪。使用UnityWebRequest的时候,你需要精心准备,比如确定URL、配置请求类型和头信息;发起请求;巧妙处理钓获的数据;还需要机智面对网络波澜,处理各种可能出现的错误。按照这样的过程,数据的钓取将会是一次既轻松愉快也效率高效的编程钓鱼之旅。
183 18
|
2月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
129 0
|
3月前
|
存储 缓存 前端开发
http协议调试代理工具,Fiddler免费版下载,抓包工具使用教程
Fiddler是一款功能强大的HTTP协议调试代理工具,能记录并检查电脑与互联网间的HTTP通信,支持断点设置和数据编辑。相比其他网络调试器,Fiddler操作更简单且用户友好,支持查看Cookie、HTML、JS、CSS等文件内容。它还具备HTTPS抓包、过滤设置、统计页面总重量等功能,适用于安全测试与功能测试。通过插件扩展,用户可自定义视图或分析缓存行为。支持多种HTTP请求方法(如GET、POST等)及状态码分类(1xx-5xx),是开发者调试网络请求的得力工具。同类工具有HttpWatch、Firebug、Wireshark等。
350 1
|
5月前
|
存储 C++
UE5 C++:自定义Http节点获取Header数据
综上,通过为UE5创建一个自定义HTTP请求类并覆盖GetResult方法,就能成功地从HTTP响应的Header数据中提取信息。在项目中使用自定义类,不仅可以方便地访问响应头数据,也可随时使用这些信息。希望这种方法可以为你的开发过程带来便利和效益。
201 35
|
6月前
|
IDE 编译器 项目管理
Dev-C++保姆级安装教程:Win10/Win11环境配置+避坑指南(附下载验证)
Dev-C++ 是一款专为 Windows 系统设计的轻量级 C/C++ 集成开发环境(IDE),内置 MinGW 编译器与调试器,支持代码高亮、项目管理等功能。4.9.9 版本作为经典稳定版,适合初学者和教学使用。本文详细介绍其安装流程、配置方法、功能验证及常见问题解决,同时提供进阶技巧和扩展学习资源,帮助用户快速上手并高效开发。
|
10月前
|
安全 前端开发 JavaScript
利用HTTP协议进行文件上传和下载的常见方法
【10月更文挑战第25天】可以利用HTTP协议方便地实现文件的上传和下载功能,满足不同应用场景下的需求。在实际应用中,还可以根据具体的业务需求和安全要求,对文件上传和下载的过程进行进一步的优化和安全处理。
|
11月前
|
Linux 开发工具 C语言
【c++】c++发送http请求
【c++】c++发送http请求
|
11月前
|
数据采集 Web App开发 数据安全/隐私保护
User-Agent在C++ HTTP请求中的作用
User-Agent在C++ HTTP请求中的作用
|
传感器 机器学习/深度学习
如何下载DVS Gesture数据集?解决tonic.datasets.DVSGesture错误HTTP Error 403: Forbidden
本文介绍了如何解决在使用tonic库下载DVSGesture数据集时遇到的HTTP Error 403 Forbidden错误,建议从Figshare的链接下载完整数据集并解压到指定目录,以便成功加载数据集进行手势识别研究。
343 1