免费注册 查看新帖 |

Chinaunix

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

[网络子系统] linux c获取网页的问题 [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-20 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-17 15:25 |只看该作者 |倒序浏览
最近学习网络编程,从网上找了个抓取网页的代码,但是不知为什么不能够获取整个网页,代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <string.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <string.h>

  8. #define  HTTPPORT 80

  9. char *head = "GET /about HTTP/1.1\r\n"
  10.             "Accept: */*\r\n"
  11.             "Accept-Language:zh-cn\r\n"
  12.             "Accept-Encoding:gzip,deflate\r\n"
  13.             "Host:www.ietf.org\r\n"
  14.             "Connection:Keep-Alive\r\n\n";
  15. int connect_URL(char *domain,int port)
  16. {
  17.     int sock;
  18.     struct hostent *host;
  19.     struct sockaddr_in server;
  20.     host = gethostbyname(domain);
  21.     if(host == NULL)
  22.     {
  23.         printf("gethostbyname error\n");
  24.         return -2;
  25.     }

  26.     sock = socket(AF_INET,SOCK_STREAM,0);
  27.     if(sock < 0)
  28.     {
  29.         printf("invalid socket\n");
  30.         return -1;
  31.     }
  32.     memset(&server,0,sizeof(struct sockaddr_in));
  33.     memcpy(&server.sin_addr,host->h_addr_list[0],host->h_length);
  34.     server.sin_family = AF_INET;
  35.     server.sin_port = htons(port);
  36.     if(connect(sock,(struct sockaddr *) &server,sizeof(struct sockaddr)) < 0 )
  37.         return -1;
  38.     else
  39.         return sock;
  40. }

  41. int main()
  42. {
  43.     int sock;
  44.     int n;
  45.     char buf[1024];
  46.     char *domain ="www.ietf.org";
  47.     FILE *fp;
  48.    
  49.     fp = fopen("test.txt","w");
  50.     if(NULL == fp)
  51.     {
  52.         printf("can't open stockcode file\n");
  53.         return -1;
  54.     }
  55.     sock = connect_URL(domain,HTTPPORT);
  56.     if(sock < 0)
  57.     {
  58.         printf("connect errror\n");
  59.         return -1;
  60.     }

  61.     send(sock,head,strlen(head),0);
  62.     while(1)
  63.     {
  64.         if((n=recv(sock,buf,1024,MSG_WAITALL)) < 1)
  65.             break;
  66.         printf("%d\n",n);
  67.         fprintf(fp,"%s",buf);
  68.     }
  69.     fclose(fp);
  70.     close(sock);
  71.     printf("bye\n");
  72.     return 0;
  73. }

  74.    
复制代码
求大神们帮忙看一下

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-20 06:20:00
2 [报告]
发表于 2012-10-17 15:27 |只看该作者
还有个问题是,如果服务器发送的数据太大,无法一次接受,那应该怎么办?

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-20 06:20:00
3 [报告]
发表于 2012-10-17 15:51 |只看该作者
自己顶。。。

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
4 [报告]
发表于 2012-10-17 22:42 |只看该作者
回复 1# goingstudy
看一下 wget 的源代码吧,或者可以考虑使用 libcurl


   

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-20 06:20:00
5 [报告]
发表于 2012-10-17 23:24 |只看该作者
谢谢,知道怎么了

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-12-20 06:20:00
6 [报告]
发表于 2012-10-17 23:25 |只看该作者
应该是网址的原因,会重定向
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP