久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2200|回復: 0
收起左側

HTTP協議的C語言編程實現

[復制鏈接]
ID:146809 發表于 2021-5-24 09:06 | 顯示全部樓層 |閱讀模式
  1. /******* http客戶端程序 httpclient.c ************/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <netinet/in.h>
  10. #include <limits.h>
  11. #include <netdb.h>
  12. #include <arpa/inet.h>
  13. #include <ctype.h>

  14. //////////////////////////////httpclient.c 開始///////////////////////////////////////////

  15. /********************************************
  16. 功能:搜索字符串右邊起的第一個匹配字符
  17. ********************************************/
  18. char * Rstrchr(char * s, char x)  {
  19.   int i = strlen(s);
  20.   if(!(*s))  return 0;
  21.   while(s[i-1]) if(strchr(s + (i - 1), x))  return (s + (i - 1));  else i--;
  22.   return 0;
  23. }

  24. /********************************************
  25. 功能:把字符串轉換為全小寫
  26. ********************************************/
  27. void ToLowerCase(char * s)  {
  28.   while(*s)  *s=tolower(*s++);
  29. }

  30. /**************************************************************
  31. 功能:從字符串src中分析出網站地址和端口,并得到用戶要下載的文件
  32. ***************************************************************/
  33. void GetHost(char * src, char * web, char * file, int * port)  {
  34.   char * pA;
  35.   char * pB;
  36.   memset(web, 0, sizeof(web));
  37.   memset(file, 0, sizeof(file));
  38.   *port = 0;
  39.   if(!(*src))  return;
  40.   pA = src;
  41.   if(!strncmp(pA, "http://", strlen("http://")))  pA = src+strlen("http://");
  42.   else if(!strncmp(pA, "https://", strlen("https://")))  pA = src+strlen("https://");
  43.   pB = strchr(pA, '/');
  44.   if(pB)  {
  45.     memcpy(web, pA, strlen(pA) - strlen(pB));
  46.     if(pB+1)  {
  47.       memcpy(file, pB + 1, strlen(pB) - 1);
  48.       file[strlen(pB) - 1] = 0;
  49.     }
  50.   }
  51.   else  memcpy(web, pA, strlen(pA));
  52.   if(pB)  web[strlen(pA) - strlen(pB)] = 0;
  53.   else  web[strlen(pA)] = 0;
  54.   pA = strchr(web, ':');
  55.   if(pA)  *port = atoi(pA + 1);
  56.   else *port = 80;
  57. }

  58. /*********************************************************************
  59. *filename: httpclient.c
  60. *purpose: HTTP協議客戶端程序,可以用來下載網頁
  61. *wrote by: zhoulifa(zhoulifa@163.com) 周立發(http://zhoulifa.bokee.com)
  62.            Linux愛好者 Linux知識傳播者 SOHO族 開發者 最擅長C語言
  63. *date time:2006-03-11 21:49:00
  64. *Note: 任何人可以任意復制代碼并運用這些代碼,當然包括你的商業用途
  65. *                         但請遵循GPL
  66. *********************************************************************/
  67. int main(int argc, char *argv[])
  68. {
  69.   int sockfd;
  70.   char buffer[1024];
  71.   struct sockaddr_in server_addr;
  72.   struct hostent *host;
  73.   int portnumber,nbytes;
  74.   char host_addr[256];
  75.   char host_file[1024];
  76.   char local_file[256];
  77.   FILE * fp;
  78.   char request[1024];
  79.   int send, totalsend;
  80.   int i;
  81.   char * pt;

  82.   if(argc!=2)
  83.   {
  84.     fprintf(stderr,"Usage:%s web-address\a\n",argv[0]);
  85.     exit(1);
  86.   }
  87.   printf("parameter.1 is: %s\n", argv[1]);
  88.   ToLowerCase(argv[1]);/*將參數轉換為全小寫*/
  89.   printf("lowercase parameter.1 is: %s\n", argv[1]);

  90.   GetHost(argv[1], host_addr, host_file, &portnumber);/*分析網址、端口、文件名等*/
  91.   printf("webhost:%s\n", host_addr);
  92.   printf("hostfile:%s\n", host_file);
  93.   printf("portnumber:%d\n\n", portnumber);

  94.   if((host=gethostbyname(host_addr))==NULL)/*取得主機IP地址*/
  95.   {
  96.     fprintf(stderr,"Gethostname error, %s\n", strerror(errno));
  97.     exit(1);
  98.   }

  99.   /* 客戶程序開始建立 sockfd描述符 */
  100.   if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET連接*/
  101.   {
  102.     fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
  103.     exit(1);
  104.   }

  105.   /* 客戶程序填充服務端的資料 */
  106.   bzero(&server_addr,sizeof(server_addr));
  107.   server_addr.sin_family=AF_INET;
  108.   server_addr.sin_port=htons(portnumber);
  109.   server_addr.sin_addr=*((struct in_addr *)host->h_addr);

  110.   /* 客戶程序發起連接請求 */
  111.   if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*連接網站*/
  112.   {
  113.     fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
  114.     exit(1);
  115.   }

  116.   sprintf(request, "GET /%s HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\n\
  117. User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
  118. Host: %s:%d\r\nConnection: Close\r\n\r\n", host_file, host_addr, portnumber);
  119.   printf("%s", request);/*準備request,將要發送給主機*/

  120.   /*取得真實的文件名*/
  121.   if(host_file && *host_file)  pt = Rstrchr(host_file, '/');
  122.   else pt = 0;

  123.   memset(local_file, 0, sizeof(local_file));
  124.   if(pt && *pt)  {
  125.     if((pt + 1) && *(pt+1))  strcpy(local_file, pt + 1);
  126.     else  memcpy(local_file, host_file, strlen(host_file) - 1);
  127.   }
  128.   else if(host_file && *host_file)  strcpy(local_file, host_file);
  129.   else  strcpy(local_file, "index.html");
  130.   printf("local filename to write:%s\n\n", local_file);

  131.   /*發送http請求request*/
  132.   send = 0;totalsend = 0;
  133.   nbytes=strlen(request);
  134.   while(totalsend < nbytes) {
  135.     send = write(sockfd, request + totalsend, nbytes - totalsend);
  136.     if(send==-1)  {printf("send error!%s\n", strerror(errno));exit(0);}
  137.     totalsend+=send;
  138.     printf("%d bytes send OK!\n", totalsend);
  139.   }

  140.   fp = fopen(local_file, "a");
  141.   if(!fp)  {
  142.     printf("create file error! %s\n", strerror(errno));
  143.     return 0;
  144.   }
  145.   printf("\nThe following is the response header:\n");
  146.   i=0;
  147.   /* 連接成功了,接收http響應,response */
  148.   while((nbytes=read(sockfd,buffer,1))==1)
  149.   {
  150.     if(i < 4)  {
  151.       if(buffer[0] == '\r' || buffer[0] == '\n')  i++;
  152.       else i = 0;
  153.       printf("%c", buffer[0]);/*把http頭信息打印在屏幕上*/
  154.     }
  155.     else  {
  156.       fwrite(buffer, 1, 1, fp);/*將http主體信息寫入文件*/
  157.       i++;
  158.       if(i%1024 == 0)  fflush(fp);/*每1K時存盤一次*/
  159.     }
  160.   }
  161.   fclose(fp);
  162.   /* 結束通訊 */
  163.   close(sockfd);
  164.   exit(0);
  165. }
復制代碼


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 超碰最新在线 | 国产精品高潮呻吟 | 日日操日日干 | 特一级黄色毛片 | 欧美一区二区在线观看 | 国产一区二区三区四区五区3d | 久久久久国色av免费观看性色 | 国产精品欧美一区二区 | 日韩高清一区二区 | 人人爽人人爽人人片av | 日本亚洲欧美 | 天堂av在线影院 | 国产免费拔擦拔擦8x高清 | 国产精品视频一区二区三区 | 日本一区二区在线视频 | av一级 | 亚洲一二三区不卡 | 日韩精品亚洲专区在线观看 | 免费特级黄毛片 | 91精品国产综合久久香蕉麻豆 | 日韩精品在线看 | 欧美亚洲国产成人 | 成人性视频免费网站 | 在线成人av| 久久丁香 | 亚洲国产精品区 | 国产一区二区精品在线观看 | 亚洲国产成人精品女人久久久 | 日韩在线免费视频 | 亚洲一区二区在线视频 | 在线播放中文字幕 | 成人免费视频网站在线看 | 狠狠撸在线视频 | 一区二区在线免费观看 | 国产一级一片免费播放 | 九九99九九精彩46 | 国产精品五月天 | 久久一本 | 高清18麻豆 | 99精品电影 | 久久久国产一区 |