http协议get,post请求示例

想做到以上要求,需先认真了解HTTP协议各字段的含义,以下是部分较好的归纳:
HTTP一个HTTP请求报文由请求行(request line)、请求头部(header)、空行和请求数据4个部分组成,下图给出了请求报文的一般格式。
  http协议get,post请求示例_第1张图片


 (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. #define IPSTR "115.239.210.26"
  15. #define PORT 80
  16. #define BUFSIZE 4092

  17. int main(int argc, char *argv[])
  18. {

  19.      int sockfd, ret, i, h;
  20.      struct sockaddr_in servaddr;
  21.      char str1[4096], buf[BUFSIZE], *str;
  22.      socklen_t len;
  23.      fd_set t_set1;
  24.      struct timeval tv;

  25.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {

  26.               printf("socket error!\n");

  27.                exit(0);
  28.         };

  29.   

  30.         bzero(&servaddr, sizeof(servaddr));
  31.         servaddr.sin_family = AF_INET;
  32.         servaddr.sin_port = htons(PORT);

  33.       if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
  34.      
  35.       {
  36.      
  37.           printf("inet_pton error!\n");
  38.           exit(0);

  39.        }
  40.      
  41.         if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  42.        {     
  43.                 printf("connect error!\n");
  44.                 exit(0);
  45.        }
  46.                 printf("connect success \n");

  47.        memset(str1, 0, 4096);
  48.        strcat(str1, "GET / HTTP/1.0\r\n");
  49.        strcat(str1, "Accept: */*\r\n"); //image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint,
  50.        strcat(str1, "Accept-Language: cn\r\n");
  51.        strcat(str1, "User-Agent: Mozilla/4.0\r\n");
  52.        strcat(str1, "Host: www.baidu.com\r\n");
  53.        strcat(str1,"Connection: Keep-Alive\r\n");
  54.        strcat(str1, "\r\n\r\n");
  55.         printf("%s\n",str1);

  56.        ret = send(sockfd,(void *)str1,strlen(str1),0);
  57.        if (ret < 0) {
  58.                 printf("send error %d,Error message'%s'\n",errno, strerror(errno));
  59.                 exit(0);

  60.         }else{

  61.                 printf("send success ,total send %d \n", ret);
  62.         }


  63.      while(1){
  64.    
  65.                 sleep(2);
  66.                 printf("******\n");
  67.                 tv.tv_sec= 0;
  68.                 tv.tv_usec= 0;

  69.                 h= 0;
  70.                           FD_ZERO(&t_set1);

  71.                            FD_SET(sockfd, &t_set1);
  72.                 printf("--------------->1\n");
  73.                 h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
  74.                  
  75.                 printf("h = %d\n",h);
  76.                 printf("--------------->2\n");
  77.                
  78.                 if (h == 0) continue;
  79.                 if (h < 0) {
  80.                          close(sockfd);
  81.                        printf("some thing read error!\n");
  82.                        return -1;

  83.                  };
  84.     
  85.                 if (h > 0){
  86.                         memset(buf, 0, 4095);
  87.                         i= recv(sockfd, (void *)buf, 4092,0);
  88.                         printf("i = %d\n",i);
  89.                           if (i==0){

  90.                                close(sockfd);
  91.                                printf("read message find error,stop!\n");
  92.                                return -1;

  93.                        }

  94.                    printf("%s\n", buf);

  95.                  }

  96.          }

  97.          close(sockfd);
  98.          return 0;
  99. }


 

 

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. int main(int argc,char **argv)
  19. {
  20.   int sockfd,ret,i ,h;
  21.   struct sockaddr_in servaddr;
  22.   char str1[4096], str2[2048], buf[BUFSIZE], *str;
  23.   socklen_t len;
  24.   fd_set t_set1;
  25.   struct timeval tv;
  26.   
  27.    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  28.       {

  29.               printf("socket error!\n");

  30.                exit(0);
  31.       }
  32.   
  33.   
  34.   
  35.     bzero(&servaddr,sizeof(servaddr)); //描述端口信息
  36.     servaddr.sin_family = AF_INET;
  37.     servaddr.sin_port = htons(PORT);
  38.     
  39.     if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 )
  40.       {
  41.      
  42.           printf("inet_pton error!\n");
  43.           exit(0);

  44.       }
  45.     if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) //绑定端口
  46.       {     
  47.                 printf("connect error!\n");
  48.                 exit(0);

  49.       }
  50.            printf("hello me\n");
  51.            printf("connect success \n");
  52.          
  53.         
  54.           memset(str2, 0, 2048);
  55.           strcat(str2, "name = leezx\n");
  56.           strcat(str2, "code = 12345");
  57.           str=(char *)malloc(128);
  58.           len = strlen(str2);
  59.           sprintf(str, "%d", len);

  60.           memset(str1, 0, 4096);

  61.           strcat(str1, "POST /account/login HTTP/1.0 \r\n");
  62.           strcat(str1, "Host: PASSPORT.CSDN.NET\r\n");
  63.           strcat(str1, "Accept-Encoding: gzip, deflate");
  64.           strcat(str1, "Accept-Language: cn\r\n");
  65.           strcat(str1, "Accept: */*\r\n");
  66.           strcat(str1, "Content-Type: application/x-www-form-urlencoded\r\n");
  67.           strcat(str1, "Content-Length: ");
  68.           strcat(str1, str);
  69.           strcat(str1, "\n\n");

  70.    
  71.           strcat(str1, str2);
  72.           strcat(str1, "\r\n\r\n");
  73.           printf("%s\n",str1);

  74.           ret = write(sockfd,str1,strlen(str1));
  75.            if (ret < 0) {
  76.                   printf("send error!errno %d,error messege'%s'\n",errno, strerror(errno));
  77.                   exit(0);

  78.                         }
  79.             else {
  80.                  printf("send succeser total send %d bytes!\n\n", ret);
  81.                  }

  82.             FD_ZERO(&t_set1);
  83.             FD_SET(sockfd, &t_set1);
  84.             while(1){
  85.                     sleep(2);
  86.                     tv.tv_sec= 0;
  87.                     tv.tv_usec= 0;
  88.                     h= 0;
  89.                     printf("--------------->1");
  90.                     h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
  91.                    printf("--------------->2");
  92.                    //if (h == 0) continue;
  93.                      if (h < 0) {
  94.                     close(sockfd);
  95.                          printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n");
  96.                          return -1;

  97.                  }

  98.                    if (h > 0){
  99.                             memset(buf, 0, 4096);
  100.                            i= read(sockfd, buf, 4095);
  101.                            if (i==0){
  102.                                  close(sockfd);
  103.                                  printf("读取数据报文时发现远端关闭,该线程终止!\n");
  104.                                 return -1;
  105.                                      }
  106.                          printf("%s\n", buf);
  107.                             }

  108.                   }
  109.          close (sockfd);
  110.          return 0;
  111.   }

你可能感兴趣的:(http协议get,post请求示例)