linux下HttpGet、HttpPost的C++实现

54 篇文章 3 订阅

前段时间,帮朋友实现linux C实现HTTP get 及POst请求,最原先打算使用libcurl库实现。但是考虑到和其他接口通信的情况,暂时使用C 来实现.代码可以自动解析URL连接,具体看下面代码:

[cpp]  view plain  copy
  1. /*File : http.h 
  2.  *Auth : sjin 
  3.  *Date : 20141206 
  4.  *Mail : 413977243@qq.com 
  5.  */  
  6. #ifndef _MY_HTTP_H  
  7. #define _MY_HTTP_H  
  8.   
  9. #define MY_HTTP_DEFAULT_PORT 80  
  10.   
  11. char * http_get(const char *url);  
  12. char * http_post(const char *url,const char * post_str);  
  13.   
  14. #endif  

[cpp]  view plain  copy
  1. /*File : http.c 
  2.  *Auth : sjin 
  3.  *Date : 20141206 
  4.  *Mail : 413977243@qq.com 
  5.  */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <arpa/inet.h>  
  9. #include <netdb.h>  
  10. #include <string.h>  
  11.   
  12. #include "http.h"  
  13.   
  14. #define BUFFER_SIZE 1024  
  15. #define HTTP_POST "POST /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n"\  
  16.     "Content-Type:application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s"  
  17. #define HTTP_GET "GET /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n\r\n"  
  18.   
  19.   
  20. static int http_tcpclient_create(const char *host, int port){  
  21.     struct hostent *he;  
  22.     struct sockaddr_in server_addr;   
  23.     int socket_fd;  
  24.   
  25.     if((he = gethostbyname(host))==NULL){  
  26.         return -1;  
  27.     }  
  28.   
  29.    server_addr.sin_family = AF_INET;  
  30.    server_addr.sin_port = htons(port);  
  31.    server_addr.sin_addr = *((struct in_addr *)he->h_addr);  
  32.   
  33.     if((socket_fd = socket(AF_INET,SOCK_STREAM,0))==-1){  
  34.         return -1;  
  35.     }  
  36.   
  37.     if(connect(socket_fd, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1){  
  38.         return -1;  
  39.     }  
  40.   
  41.     return socket_fd;  
  42. }  
  43.   
  44. static void http_tcpclient_close(int socket){  
  45.     close(socket);  
  46. }  
  47.   
  48. static int http_parse_url(const char *url,char *host,char *file,int *port)  
  49. {  
  50.     char *ptr1,*ptr2;  
  51.     int len = 0;  
  52.     if(!url || !host || !file || !port){  
  53.         return -1;  
  54.     }  
  55.   
  56.     ptr1 = (char *)url;  
  57.   
  58.     if(!strncmp(ptr1,"http://",strlen("http://"))){  
  59.         ptr1 += strlen("http://");  
  60.     }else{  
  61.         return -1;  
  62.     }  
  63.   
  64.     ptr2 = strchr(ptr1,'/');  
  65.     if(ptr2){  
  66.         len = strlen(ptr1) - strlen(ptr2);  
  67.         memcpy(host,ptr1,len);  
  68.         host[len] = '\0';  
  69.         if(*(ptr2 + 1)){  
  70.             memcpy(file,ptr2 + 1,strlen(ptr2) - 1 );  
  71.             file[strlen(ptr2) - 1] = '\0';  
  72.         }  
  73.     }else{  
  74.         memcpy(host,ptr1,strlen(ptr1));  
  75.         host[strlen(ptr1)] = '\0';  
  76.     }  
  77.     //get host and ip  
  78.     ptr1 = strchr(host,':');  
  79.     if(ptr1){  
  80.         *ptr1++ = '\0';  
  81.         *port = atoi(ptr1);  
  82.     }else{  
  83.         *port = MY_HTTP_DEFAULT_PORT;  
  84.     }  
  85.   
  86.     return 0;  
  87. }  
  88.   
  89.   
  90. static int http_tcpclient_recv(int socket,char *lpbuff){  
  91.     int recvnum = 0;  
  92.   
  93.     recvnum = recv(socket, lpbuff,BUFFER_SIZE*4,0);  
  94.   
  95.     return recvnum;  
  96. }  
  97.   
  98. static int http_tcpclient_send(int socket,char *buff,int size){  
  99.     int sent=0,tmpres=0;  
  100.   
  101.     while(sent < size){  
  102.         tmpres = send(socket,buff+sent,size-sent,0);  
  103.         if(tmpres == -1){  
  104.             return -1;  
  105.         }  
  106.         sent += tmpres;  
  107.     }  
  108.     return sent;  
  109. }  
  110.   
  111. static char *http_parse_result(const char*lpbuf)  
  112. {  
  113.     char *ptmp = NULL;   
  114.     char *response = NULL;  
  115.     ptmp = (char*)strstr(lpbuf,"HTTP/1.1");  
  116.     if(!ptmp){  
  117.         printf("http/1.1 not faind\n");  
  118.         return NULL;  
  119.     }  
  120.     if(atoi(ptmp + 9)!=200){  
  121.         printf("result:\n%s\n",lpbuf);  
  122.         return NULL;  
  123.     }  
  124.   
  125.     ptmp = (char*)strstr(lpbuf,"\r\n\r\n");  
  126.     if(!ptmp){  
  127.         printf("ptmp is NULL\n");  
  128.         return NULL;  
  129.     }  
  130.     response = (char *)malloc(strlen(ptmp)+1);  
  131.     if(!response){  
  132.         printf("malloc failed \n");  
  133.         return NULL;  
  134.     }  
  135.     strcpy(response,ptmp+4);  
  136.     return response;  
  137. }  
  138.   
  139. char * http_post(const char *url,const char *post_str){  
  140.   
  141.     char post[BUFFER_SIZE] = {'\0'};  
  142.     int socket_fd = -1;  
  143.     char lpbuf[BUFFER_SIZE*4] = {'\0'};  
  144.     char *ptmp;  
  145.     char host_addr[BUFFER_SIZE] = {'\0'};  
  146.     char file[BUFFER_SIZE] = {'\0'};  
  147.     int port = 0;  
  148.     int len=0;  
  149.     char *response = NULL;  
  150.   
  151.     if(!url || !post_str){  
  152.         printf("      failed!\n");  
  153.         return NULL;  
  154.     }  
  155.   
  156.     if(http_parse_url(url,host_addr,file,&port)){  
  157.         printf("http_parse_url failed!\n");  
  158.         return NULL;  
  159.     }  
  160.     //printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);  
  161.   
  162.     socket_fd = http_tcpclient_create(host_addr,port);  
  163.     if(socket_fd < 0){  
  164.         printf("http_tcpclient_create failed\n");  
  165.         return NULL;  
  166.     }  
  167.        
  168.     sprintf(lpbuf,HTTP_POST,file,host_addr,port,strlen(post_str),post_str);  
  169.   
  170.     if(http_tcpclient_send(socket_fd,lpbuf,strlen(lpbuf)) < 0){  
  171.         printf("http_tcpclient_send failed..\n");  
  172.         return NULL;  
  173.     }  
  174.     //printf("发送请求:\n%s\n",lpbuf);  
  175.   
  176.     /*it's time to recv from server*/  
  177.     if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){  
  178.         printf("http_tcpclient_recv failed\n");  
  179.         return NULL;  
  180.     }  
  181.   
  182.     http_tcpclient_close(socket_fd);  
  183.   
  184.     return http_parse_result(lpbuf);  
  185. }  
  186.   
  187. char * http_get(const char *url)  
  188. {  
  189.   
  190.     char post[BUFFER_SIZE] = {'\0'};  
  191.     int socket_fd = -1;  
  192.     char lpbuf[BUFFER_SIZE*4] = {'\0'};  
  193.     char *ptmp;  
  194.     char host_addr[BUFFER_SIZE] = {'\0'};  
  195.     char file[BUFFER_SIZE] = {'\0'};  
  196.     int port = 0;  
  197.     int len=0;  
  198.   
  199.     if(!url){  
  200.         printf("      failed!\n");  
  201.         return NULL;  
  202.     }  
  203.   
  204.     if(http_parse_url(url,host_addr,file,&port)){  
  205.         printf("http_parse_url failed!\n");  
  206.         return NULL;  
  207.     }  
  208.     //printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);  
  209.   
  210.     socket_fd =  http_tcpclient_create(host_addr,port);  
  211.     if(socket_fd < 0){  
  212.         printf("http_tcpclient_create failed\n");  
  213.         return NULL;  
  214.     }  
  215.   
  216.     sprintf(lpbuf,HTTP_GET,file,host_addr,port);  
  217.   
  218.     if(http_tcpclient_send(socket_fd,lpbuf,strlen(lpbuf)) < 0){  
  219.         printf("http_tcpclient_send failed..\n");  
  220.         return NULL;  
  221.     }  
  222. //  printf("发送请求:\n%s\n",lpbuf);  
  223.   
  224.     if(http_tcpclient_recv(socket_fd,lpbuf) <= 0){  
  225.         printf("http_tcpclient_recv failed\n");  
  226.         return NULL;  
  227.     }  
  228.     http_tcpclient_close(socket_fd);  
  229.   
  230.     return http_parse_result(lpbuf);  
  231. }  





转自:http://blog.csdn.net/dezhihuang/article/details/51355639
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值