linux下get、post网页访问实现 - 小众知识

linux下get、post网页访问实现

2014-11-13 07:57:37 苏内容
  标签: linux,get,post
阅读:3662
想做到以上要求,需先认真了解HTTP协议各字段的含义,以下是部分较好的归纳:
HTTP一个HTTP请求报文由请求行(request line)、请求头部(header)、空行和请求数据4个部分组成,下图给出了请求报文的一般格式。
 


 (1)请求行
请求行由请求方法字段、URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔。例如,GET /index.html HTTP/1.1。
HTTP协议的请求方法有GET、POST、HEAD、PUT、DELETE、OPTIONS、TRACE、CONNECT。这里介绍最常用的GET方法和POST方法。
GET:当客户端要从服务器中读取文档时,使用GET方法。GET方法要求服务器将URL定位的资源放在响应报文的数据部分,回送给客户端。使用GET方法时,请求参数和对应的值附加在URL后面,利用一个问号(“?”)代表URL的结尾与请求参数的开始,传递参数长度受限制。例如,/index.jsp?id=100&op=bind。
POST:当客户端给服务器提供信息较多时可以使用POST方法。POST方法将请求参数封装在HTTP请求数据中,以名称/值的形式出现,可以传输大量数据,可用来传送文件。
(2)请求头部
请求头部由关键字/值对组成,每行一对,关键字和值用英文冒号“:”分隔。请求头部通知服务器有关于客户端请求的信息,典型的请求头有:
User-Agent:产生请求的浏览器类型。
Accept:客户端可识别的内容类型列表。
Host:请求的主机名,允许多个域名同处一个IP地址,即虚拟主机。
(3)空行
最后一个请求头之后是一个空行,发送回车符和换行符,通知服务器以下不再有请求头。
对于一个完整的http请求来说空行是必须的,否则服务器会认为本次请求的数据尚未完全发送到服务器,处于等待状态。
(4)请求数据
请求数据不在GET方法中使用,而是在POST方法中使用。POST方法适用于需要客户填写表单的场合。与请求数据相关的最常使用的请求头是Content-Type和Content-Length。
一、get访问

1、程序分析:

使用socket 编程实现get访问网页,以TCP协议绑定。

代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h> 
  2. #include <sys/socket.h>
  3. #include <sys/types.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11. #include <sys/time.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14.  
  15. #define IPSTR "115.239.210.26"
  16. #define PORT 80 
  17. #define BUFSIZE 4092 
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.  
  22.      int sockfd, ret, i, h;
  23.      struct sockaddr_in servaddr;
  24.      char str1[4096], buf[BUFSIZE], *str;
  25.      socklen_t len;
  26.      fd_set t_set1;
  27.      struct timeval tv;
  28.  
  29.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
  30.  
  31.               printf("socket error!\n");
  32.  
  33.                exit(0);
  34.         };
  35.  
  36.   
  37.  
  38.         bzero(&servaddr, sizeof(servaddr));
  39.         servaddr.sin_family = AF_INET;
  40.         servaddr.sin_port = htons(PORT);
  41.  
  42.       if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
  43.      
  44.       {
  45.      
  46.           printf("inet_pton error!\n");
  47.           exit(0);
  48.  
  49.        }
  50.      
  51.         if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  52.        {     
  53.                 printf("connect error!\n");
  54.                 exit(0);
  55.        }
  56.                 printf("connect success \n");
  57.  
  58.        memset(str1, 0, 4096);
  59.        strcat(str1, "GET / HTTP/1.0\r\n");
  60.        strcat(str1, "Accept: */*\r\n"); //image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint,
  61.        strcat(str1, "Accept-Language: cn\r\n");
  62.        strcat(str1, "User-Agent: Mozilla/4.0\r\n");
  63.        strcat(str1, "Host: www.baidu.com\r\n");
  64.        strcat(str1,"Connection: Keep-Alive\r\n");
  65.        strcat(str1, "\r\n\r\n");
  66.         printf("%s\n",str1);
  67.  
  68.        ret = send(sockfd,(void *)str1,strlen(str1),0);
  69.        if (ret < 0) {
  70.                 printf("send error %d,Error message'%s'\n",errno, strerror(errno));
  71.                 exit(0);
  72.  
  73.         }else{
  74.  
  75.                 printf("send success ,total send %d \n", ret);
  76.         }
  77.  
  78.  
  79.      while(1){
  80.    
  81.                 sleep(2);
  82.                 printf("******\n");
  83.                 tv.tv_sec= 0;
  84.                 tv.tv_usec= 0;
  85.  
  86.                 h= 0;
  87.                           FD_ZERO(&t_set1);
  88.  
  89.                            FD_SET(sockfd, &t_set1);
  90.                 printf("--------------->1\n");
  91.                 h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
  92.                  
  93.                 printf("h = %d\n",h);
  94.                 printf("--------------->2\n");
  95.                
  96.                 if (== 0) continue;
  97.                 if (< 0) {
  98.                          close(sockfd);
  99.                        printf("some thing read error!\n");
  100.                        return -1;
  101.  
  102.                  };
  103.     
  104.                 if (> 0){
  105.                         memset(buf, 0, 4095);
  106.                         i= recv(sockfd, (void *)buf, 4092,0);
  107.                         printf("i = %d\n",i);
  108.                           if (i==0){
  109.  
  110.                                close(sockfd);
  111.                                printf("read message find error,stop!\n");
  112.                                return -1;
  113.  
  114.                        }
  115.  
  116.                    printf("%s\n", buf);
  117.  
  118.                  }
  119.  
  120.          }
  121.  
  122.          close(sockfd);
  123.          return 0;
  124. }


 

 

2、使用socket编程实现post访问网页,以csdn登陆框为例。

代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h> 
  2. #include <sys/socket.h>
  3. #include <sys/types.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11. #include <sys/time.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14.    
  15. #define IPSTR "117.79.93.222"
  16. #define PORT 80 
  17. #define BUFSIZE 4096 
  18.  
  19. int main(int argc,char **argv)
  20. {
  21.   int sockfd,ret,,h;
  22.   struct sockaddr_in servaddr;
  23.   char str1[4096], str2[2048], buf[BUFSIZE], *str;
  24.   socklen_t len;
  25.   fd_set t_set1;
  26.   struct timeval tv;
  27.   
  28.    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  29.       {
  30.  
  31.               printf("socket error!\n");
  32.  
  33.                exit(0);
  34.       }
  35.   
  36.   
  37.   
  38.     bzero(&servaddr,sizeof(servaddr)); //描述端口信息
  39.     servaddr.sin_family = AF_INET;
  40.     servaddr.sin_port = htons(PORT);
  41.     
  42.     if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
  43.       {
  44.      
  45.           printf("inet_pton error!\n");
  46.           exit(0);
  47.  
  48.       }
  49.     if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) //绑定端口 
  50.       {     
  51.                 printf("connect error!\n");
  52.                 exit(0);
  53.  
  54.       }
  55.            printf("hello me\n");
  56.            printf("connect success \n");
  57.          
  58.         
  59.           memset(str2, 0, 2048);
  60.           strcat(str2, "name = leezx\n");
  61.           strcat(str2, "code = 12345");
  62.           str=(char *)malloc(128);
  63.           len = strlen(str2);
  64.           sprintf(str, "%d", len);
  65.  
  66.           memset(str1, 0, 4096);
  67.  
  68.           strcat(str1, "POST /account/login HTTP/1.0 \r\n");
  69.           strcat(str1, "Host: PASSPORT.CSDN.NET\r\n");
  70.           strcat(str1, "Accept-Encoding: gzip, deflate");
  71.           strcat(str1, "Accept-Language: cn\r\n");
  72.           strcat(str1, "Accept: */*\r\n");
  73.           strcat(str1, "Content-Type: application/x-www-form-urlencoded\r\n");
  74.           strcat(str1, "Content-Length: ");
  75.           strcat(str1, str);
  76.           strcat(str1, "\n\n");
  77.  
  78.    
  79.           strcat(str1, str2);
  80.           strcat(str1, "\r\n\r\n");
  81.           printf("%s\n",str1);
  82.  
  83.           ret = write(sockfd,str1,strlen(str1));
  84.            if (ret < 0) {
  85.                   printf("send error!errno %d,error messege'%s'\n",errno, strerror(errno));
  86.                   exit(0);
  87.  
  88.                         }
  89.             else {
  90.                  printf("send succeser total send %d bytes!\n\n", ret);
  91.                  }
  92.  
  93.             FD_ZERO(&t_set1);
  94.             FD_SET(sockfd, &t_set1);
  95.             while(1){
  96.                     sleep(2);
  97.                     tv.tv_sec= 0;
  98.                     tv.tv_usec= 0;
  99.                     h= 0;
  100.                     printf("--------------->1");
  101.                     h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
  102.                    printf("--------------->2");
  103.                    //if (== 0) continue;
  104.                      if (< 0) {
  105.                     close(sockfd);
  106.                          printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n");
  107.                          return -1;
  108.  
  109.                  }
  110.  
  111.                    if (> 0){
  112.                             memset(buf, 0, 4096);
  113.                            i= read(sockfd, buf, 4095);
  114.                            if (i==0){
  115.                                  close(sockfd);
  116.                                  printf("读取数据报文时发现远端关闭,该线程终止!\n");
  117.                                 return -1;
  118.                                      }
  119.                          printf("%s\n", buf);
  120.                             }
  121.  
  122.                   }
  123.          close (sockfd);
  124.          return 0;
  125.   }
扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1