Linux下发送HTTP协议请求

54 篇文章 3 订阅

 在Linux系统中用C语言实现的HTTP协议的POST和GET请求,下面是代码实现,如果要测试需要搭建个后台服务器的环境,

作者测试用的是PHP开发环境,具体搭建可参看另一篇文章:http://blog.csdn.net/hanbo622/article/details/51598648

demo.c

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #include <netdb.h>  
  6. #include <arpa/inet.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. int main(int argc, char *argv[])  
  10. {  
  11.     unsigned short port = 80;           // 服务器的端口号  
  12.     char *server_ip = "192.168.2.203";  // 服务器ip地址  
  13.   
  14.     int sockfd = socket(AF_INET, SOCK_STREAM, 0);// 创建TCP套接字  
  15.     if(sockfd < 0)  
  16.     {  
  17.         perror("socket");  
  18.         exit(-1);  
  19.     }  
  20.       
  21.     struct sockaddr_in server_addr; //定义服务器信息结构体  
  22.     bzero(&server_addr,sizeof(server_addr));   
  23.     server_addr.sin_family = AF_INET;  
  24.     server_addr.sin_port = htons(port);  
  25.     inet_pton(AF_INET, server_ip, &server_addr.sin_addr);  
  26.       
  27.     int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));   // 主动连接服务器  
  28.     if(err_log != 0)  
  29.     {  
  30.         perror("connect");  
  31.         close(sockfd);  
  32.         exit(-1);  
  33.     }  
  34.       
  35.     char str1[1024] = "";  
  36.     char data[]="username=hanbo&num=123"//POST 数据  
  37. #if 1    //POST 格式请求  
  38.     sprintf(str1, "%s\r\n","POST http://192.168.2.203/index.php HTTP/1.1"); //http://192.168.2.203/index.php 服务端接收数据处理文件地址  
  39.     sprintf(str1, "%s%s\r\n",str1,"Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*");  
  40.     sprintf(str1, "%s%s\r\n",str1,"Referer: */*");  
  41.     sprintf(str1, "%s%s\r\n",str1,"Accept-Language: en-US,zh-CN;q=0.5");  
  42.     sprintf(str1, "%s%s\r\n",str1,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)");  
  43.     sprintf(str1, "%s%s\r\n",str1,"Content-Type: application/x-www-form-urlencoded");  
  44.     sprintf(str1, "%s%s\r\n",str1,"Accept-Encoding: gzip, deflate");  
  45.     sprintf(str1, "%s%s\r\n",str1,"Host: 192.168.2.203"); //服务器地址  
  46.     sprintf(str1, "%s%s%d\r\n",str1,"Content-Length: ",strlen(data)); //数据长度  
  47.     sprintf(str1, "%s%s\r\n",str1,"Connection: Keep-Alive");  
  48.     sprintf(str1,"%s%s\r\n",str1,"Cache-Control: no-cache");  
  49.     sprintf(str1, "%s\r\n",str1); //多加个回车  
  50.     sprintf(str1, "%s%s\r\n",str1,data);  
  51. #else     //GET 格式请求  
  52.     sprintf(str1, "%s\r\n","GET http://192.168.2.203/index.php?username=hanbo&num=123 HTTP/1.1"); //服务端接收数据处理文件地址,并带参数  
  53.     sprintf(str1, "%s%s\r\n",str1,"Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*");  
  54.     sprintf(str1, "%s%s\r\n",str1,"Accept-Language: en-US,zh-CN;q=0.5");  
  55.     sprintf(str1, "%s%s\r\n",str1,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)");  
  56.     sprintf(str1, "%s%s\r\n",str1,"Accept-Encoding: gzip, deflate");  
  57.     sprintf(str1, "%s%s\r\n",str1,"Host: 192.168.2.203"); //服务器地址  
  58.     sprintf(str1, "%s%s\r\n",str1,"Connection: Keep-Alive");  
  59.     sprintf(str1,"%s%s\r\n",str1,"Cookie: JSESSIONID=5386A9443729D7EB0B61E38A9C7CF52F");  
  60.     sprintf(str1, "%s\r\n",str1);     
  61. #endif  
  62.     printf("----------------------------- HTTP Data ----------------------------------\n");  
  63.     printf("%s",str1);  
  64.     printf("--------------------------- Data Len=%d ----------------------------------\n\n",strlen(str1));  
  65.       
  66.     int ret=send(sockfd, str1, strlen(str1), 0);   // 向服务器发送信息  
  67.     if(ret<0)  
  68.     {  
  69.         perror("send");  
  70.         close(sockfd);  
  71.         exit(-1);  
  72.     }  
  73.       
  74.     char recv_buf[521]="";  
  75.     recv(sockfd, recv_buf, sizeof(recv_buf), 0);  
  76.     printf("------------------------ server retrun data -------------------------------\n");  
  77.     printf("%s\n\n",recv_buf);  
  78.     close(sockfd);  
  79.   
  80.     return 0;  
  81. }  


在搭建好的服务端,在Apache安装目录D:\AppServ\Apache24\htdocs 中新建文件index.php内容如下:

[php]  view plain  copy
  1. <?php  
  2. /* 接收POST 格式数据处理 */  
  3. echo "username=".$_POST['username'];  
  4. echo "\nnum=".$_POST['num'];  
  5.   
  6. /* 接收GET 格式数据处理 */  
  7. // echo "username=".$_GET['username'];  
  8. // echo "\nnum=".$_GET['num'];  
  9. ?>  

运行POST请求会出现如下结果:

[cpp]  view plain  copy
  1. ----------------------------- HTTP Data ----------------------------------  
  2. POST http://192.168.2.203/index.php HTTP/1.1  
  3. Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */* 
  4. Referer: */*  
  5. Accept-Language: en-US,zh-CN;q=0.5  
  6. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)  
  7. Content-Type: application/x-www-form-urlencoded  
  8. Accept-Encoding: gzip, deflate  
  9. Host: 192.168.2.203  
  10. Content-Length: 22  
  11. Connection: Keep-Alive  
  12. Cache-Control: no-cache  
  13.   
  14. username=hanbo&num=123  
  15. --------------------------- Data Len=701 ----------------------------------  
  16.   
  17. ------------------------ server retrun data -------------------------------  
  18. HTTP/1.1 200 OK  
  19. Date: Mon, 06 Jun 2016 02:40:28 GMT  
  20. Server: Apache/2.4.10 (Win32) PHP/5.5.25  
  21. X-Powered-By: PHP/5.5.25  
  22. Content-Length: 22  
  23. Keep-Alive: timeout=5, max=100  
  24. Connection: Keep-Alive  
  25. Content-Type: text/html  
  26.   
  27. username=hanbo  
  28. num=123  


运行GET请求会出现如下结果:

[cpp]  view plain  copy
  1. ----------------------------- HTTP Data ----------------------------------  
  2. GET http://192.168.2.203/index.php?username=hanbo&num=123 HTTP/1.1  
  3. Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*  
  4. Accept-Language: en-US,zh-CN;q=0.5  
  5. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qdesk 2.4.1265.203; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3)  
  6. Accept-Encoding: gzip, deflate  
  7. Host: 192.168.2.203  
  8. Connection: Keep-Alive  
  9. Cookie: JSESSIONID=5386A9443729D7EB0B61E38A9C7CF52F  
  10.   
  11. --------------------------- Data Len=644 ----------------------------------  
  12.   
  13. ------------------------ server retrun data -------------------------------  
  14. HTTP/1.1 200 OK  
  15. Date: Mon, 06 Jun 2016 02:42:52 GMT  
  16. Server: Apache/2.4.10 (Win32) PHP/5.5.25  
  17. X-Powered-By: PHP/5.5.25  
  18. Content-Length: 22  
  19. Keep-Alive: timeout=5, max=100  
  20. Connection: Keep-Alive  
  21. Content-Type: text/html  
  22.   
  23. username=hanbo  
  24. num=123  


转自: http://blog.csdn.net/hanbo622/article/details/51598724

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值