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. }  
目录
相关文章
|
4月前
|
程序员 编译器 C语言
最新Dev-C++下载安装以及C语言环境搭建教程(含C语言入门教程)
最新Dev-C++下载安装以及C语言环境搭建教程(含C语言入门教程)
239 0
|
1月前
|
XML JSON JavaScript
推荐一个比较好用的c++版本http协议库-cpp-httplib
推荐一个比较好用的c++版本http协议库-cpp-httplib
50 1
|
3月前
|
存储 测试技术 C++
C++基于多态的职工管理系统(附代码下载)
C++基于多态的职工管理系统(附代码下载)
|
4月前
|
网络协议 C++
C++异步网络库workflow入门教程(1)HTTP任务
创建任务方法原型 在workflow中所有的客户端任务都放在`WFTaskFactory`工厂类中 + `url:`请求的http url + `redirect_max:`表示最大重定向次数。如果在请求过程中遇到重定向,该参数指定了最多允许重定向的次数。 + `retry_max`:表示最大重试次数。如果请求失败,该参数指定了最多可以重试的次数。 + `callback`:这是一个回调函数的指针,用于处理请求的响应。原型为`using http_callback_t = std::function
75 0
|
4月前
【基于C++HTTP 服务器的epoll 改造】
【基于C++HTTP 服务器的epoll 改造】
|
4月前
|
Web App开发 网络协议 Linux
Linux C/C++ 开发(学习笔记十 ):实现http请求器(TCP客户端)
Linux C/C++ 开发(学习笔记十 ):实现http请求器(TCP客户端)
59 0
|
4月前
|
数据安全/隐私保护 C++
c++实现http客户端和服务端的开源库以及Base64加密密码
c++实现http客户端和服务端的开源库以及Base64加密密码
|
5月前
|
存储 API 分布式数据库
C/C++ 通过HTTP实现文件上传下载
WinInet(Windows Internet)是 Microsoft Windows 操作系统中的一个 API 集,用于提供对 Internet 相关功能的支持。它包括了一系列的函数,使得 Windows 应用程序能够进行网络通信、处理 HTTP 请求、FTP 操作等。WinInet 提供了一套完整的网络通信工具,使得开发者能够轻松地构建支持网络功能的应用程序,涵盖了从简单的 HTTP 请求到复杂的文件传输等多种网络操作。
106 1
C/C++ 通过HTTP实现文件上传下载
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在线上往hbase导数据,因为hbase写入能力比较强,没有太在意写的问题。让业务方进行历史数据的导入操作,中间发现一个问题,写入速度太快,并且业务数据集中到其中一个region,这个region无法split掉,处于不可用状态。
1305 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Datanode的日志中看到: 10/12/14 20:10:31 INFO hdfs.DFSClient: Could not obtain block blk_XXXXXXXXXXXXXXXXXXXXXX_YYYYYYYY from any node: java.
662 0