免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4394 | 回复: 18
打印 上一主题 下一主题

[C] 求助:: c程序通过http访问web server的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-16 10:54 |只看该作者 |倒序浏览
哪位仁兄有linux c程序通过http访问web server(文件下载)方面的资料啊
谢谢!!!!!!

linux c程序通过http向web server发请求,web server端响应这个请求
举例就是:通过c程序发个取文件请求给web server,web 端做出响应,并生成相应的文件,是不是可以通过http来把文件取过来?
有没有这方面的资料
谢谢

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
2 [报告]
发表于 2008-07-16 11:04 |只看该作者
http协议
libcurl

论坛徽章:
0
3 [报告]
发表于 2008-07-16 11:06 |只看该作者
先用nc 手动实现整个过程,然后,把nc的链接部分,用自己的socket代替

论坛徽章:
0
4 [报告]
发表于 2008-07-16 11:06 |只看该作者
貌似熟悉HTTP协议的话,直接发个buffer就成了

论坛徽章:
0
5 [报告]
发表于 2008-07-16 11:07 |只看该作者
谢谢楼上
有没有具体的实现案例?
我知道linux 下 libcurl可以实现这个功能但不知道具体的实现过程

论坛徽章:
0
6 [报告]
发表于 2008-07-16 11:11 |只看该作者
看看手册。Google一下,找找别人做好的例子

论坛徽章:
0
7 [报告]
发表于 2008-07-16 11:12 |只看该作者
  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([email]zhoulifa@163.com[/email]) 周立发([url]http://zhoulifa.bokee.com[/url])
  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. }
  166. //////////////////////////////httpclient.c 结束///////////////////////////////////////////
复制代码

论坛徽章:
0
8 [报告]
发表于 2008-07-16 11:14 |只看该作者
程序的具体过程就是,发个buferr(GET),然后接受,写盘……

论坛徽章:
0
9 [报告]
发表于 2008-07-16 11:16 |只看该作者
哈哈,偶最近也在研究这一块东西,http://www.mawudi.cn/html/19.html这里有例子,哈哈!

小马哥寒舍http://www.mawudi.cn

论坛徽章:
0
10 [报告]
发表于 2008-07-16 11:19 |只看该作者
说白了就是:socket通讯对不?
那web server 端允许我通过http取文件吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP