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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2157|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

HTTP協(xié)議的C語言編程實現(xiàn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:146809 發(fā)表于 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. 功能:把字符串轉(zhuǎn)換為全小寫
  26. ********************************************/
  27. void ToLowerCase(char * s)  {
  28.   while(*s)  *s=tolower(*s++);
  29. }

  30. /**************************************************************
  31. 功能:從字符串src中分析出網(wǎng)站地址和端口,并得到用戶要下載的文件
  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協(xié)議客戶端程序,可以用來下載網(wǎng)頁
  61. *wrote by: zhoulifa(zhoulifa@163.com) 周立發(fā)(http://zhoulifa.bokee.com)
  62.            Linux愛好者 Linux知識傳播者 SOHO族 開發(fā)者 最擅長C語言
  63. *date time:2006-03-11 21:49:00
  64. *Note: 任何人可以任意復(fù)制代碼并運用這些代碼,當(dāng)然包括你的商業(yè)用途
  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]);/*將參數(shù)轉(zhuǎn)換為全小寫*/
  89.   printf("lowercase parameter.1 is: %s\n", argv[1]);

  90.   GetHost(argv[1], host_addr, host_file, &portnumber);/*分析網(wǎng)址、端口、文件名等*/
  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.   /* 客戶程序填充服務(wù)端的資料 */
  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.   /* 客戶程序發(fā)起連接請求 */
  111.   if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*連接網(wǎng)站*/
  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);/*準(zhǔn)備request,將要發(fā)送給主機*/

  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.   /*發(fā)送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響應(yīng),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.   /* 結(jié)束通訊 */
  163.   close(sockfd);
  164.   exit(0);
  165. }
復(fù)制代碼


評分

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

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 久久国产一区二区三区 | 亚洲一区二区三区免费在线 | 精品视频一区二区 | 亚洲国产成人精品久久久国产成人一区 | 精品欧美一区免费观看α√ | 国产成人免费视频网站高清观看视频 | 日韩a | 精品久久香蕉国产线看观看亚洲 | 国产精品久久久久久久久图文区 | 羞羞视频免费在线 | 欧美在线一区二区三区四区 | 日韩欧美在线不卡 | 国产综合在线视频 | 欧美精品一级 | 欧美一区二区免费电影 | 视频一区二区中文字幕 | 久久精品黄色 | 欧美一区不卡 | 有码在线| 国产一级视频在线播放 | av日韩一区 | 福利影院在线看 | 亚洲a在线视频 | 性高湖久久久久久久久aaaaa | 日批免费看 | 精品国产免费人成在线观看 | 亚洲视频免费观看 | 欧美bondage紧缚视频 | 免费看色 | 成人一级黄色毛片 | 日韩国产在线观看 | 国产午夜精品视频 | 中国一级特黄真人毛片免费观看 | 日本视频在线播放 | 在线免费观看黄视频 | 日韩中文字幕网 | 亚洲图片视频一区 | 亚洲精品久久国产高清情趣图文 | 精品伊人| 99在线免费观看视频 | 欧美在线综合 |