免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: cc007c
打印 上一主题 下一主题

[C] 给定url集合,如何用C语言编程实现去下载网页源代码 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-04-10 22:50 |只看该作者
照http/1.1来写啊,一般用
GET url http/1.1
就可以获得该网页(源码)了。至于具体的socket编程,要看UNP哇~

论坛徽章:
0
12 [报告]
发表于 2007-04-11 08:38 |只看该作者

  1. /*
  2.    Wget V. 0.7 Download File Form Url
  3.    
  4.    by wzt <[email]wzt@xsec.org[/email]>
  5.    
  6. */

  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. #include <time.h>

  16. #define MAXLINE 65535
  17. #define SENDDATA 1000

  18. #define PORT 80

  19. struct hostent *host;

  20. char *data_m="Accept: */*\r\n"
  21.         "Accept-Language: zh-cn\r\n"
  22.         "UA-CPU: x86\r\n"
  23.         "Accept-Encoding: gzip, deflate\r\n"
  24.         "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; TencentTraveler ; .NET CLR 1.1.4322)\r\n";
  25.       
  26.       
  27. char *data_e="Connection: Keep-Alive\r\n\r\n";

  28. typedef struct DWONLOAD_FILE{
  29.    char hosts[100];
  30.    char ports[200];
  31.    char file[200];
  32.    int port;
  33. }DOWN;

  34. void usage(char *pro);
  35. void ver();
  36. DOWN abstract_host(char *src);
  37. void download_file(char *url_t,char *local_file);
  38. int connect_server(int port);

  39. void usage(char *pro)
  40. {
  41.    ver();
  42.    printf("usage : %s <remote file> <local file>\n",pro);
  43.    printf("exp: %s [url]http://tthacker.sitesled.com/backdoor/bindtty.c[/url] bindtty.c\n",pro);
  44.    exit(0);
  45. }

  46. void ver()
  47. {
  48.    printf("\nWget Ver 0.6 by W.Z.T, [email]wzt@xsec.org[/email] 2006/10/27\n");
  49. }

  50. int connect_server(int port)
  51. {
  52.    struct sockaddr_in remote;
  53.    int sock_id;
  54.    
  55.    if((sock_id=socket(AF_INET,SOCK_STREAM,0))<0){
  56.        perror("sock_id");
  57.        exit(0);
  58.    }
  59.    
  60.    remote.sin_family=AF_INET;
  61.    remote.sin_port=htons(port);
  62.    remote.sin_addr=*((struct in_addr *)host->h_addr);
  63.    
  64.    if(connect(sock_id,(struct sockaddr *)&remote,sizeof(struct sockaddr))<0){
  65.        perror("connect");
  66.        return -1;
  67.    }
  68.    
  69.    printf("\n[+] Connect :%s:%d OK!\r\n",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port));
  70.    
  71.    return sock_id;
  72. }

  73. void download_file(char *url_t,char *local_file)
  74. {
  75.    DOWN file_get;
  76.    FILE *fp;
  77.    time_t t_s,t_e;
  78.    char download_url[SENDDATA];
  79.    char recv_data[MAXLINE];
  80.    int sock_fd;
  81.    long get_file_len=0;
  82.    int r_len;
  83.    int http_check;
  84.    char *real_data_pos;
  85.       
  86.    ver();
  87.    file_get=abstract_host(url_t);

  88.    printf("\n[+] Host :%s\n",file_get.hosts);
  89.    printf("[+] Port :%d\n",file_get.port);
  90.    printf("[+] File :%s\n",file_get.file);
  91.    
  92.    
  93.    if((fp=fopen(local_file,"w+"))==NULL){
  94.        printf("\n[-] Create Local File %s Failed.\n",local_file);
  95.        exit(1);
  96.    }
  97.    
  98.    if((host=gethostbyname(file_get.hosts))==NULL){
  99.        herror("gethostbyname");
  100.        exit(0);
  101.    }

  102. // 提交url

  103.    sprintf(download_url,"GET %s HTTP/1.1\r\n%sHost: %s\r\n%s",file_get.file,data_m,file_get.hosts,data_e);
  104.    
  105. // printf("%s",download_url);

  106.    
  107.    sock_fd=connect_server(file_get.port);
  108.    
  109.    if(sock_fd==-1){
  110.        printf("connect error.\n");
  111.        exit(1);
  112.    }

  113.    time(&t_s);
  114.    printf("\n[+] Starting Download at %s",ctime(&t_s)+4);
  115.       
  116. // 接受并处理第-批数据

  117.    
  118.    write(sock_fd,download_url,strlen(download_url));
  119.    r_len=read(sock_fd,recv_data,MAXLINE);
  120.    
  121. // printf("%s",recv_data);

  122.    http_check=atoi(recv_data+9);
  123.    
  124. // printf("%d\n",http_check);

  125.    if(http_check == 200 || http_check == 206){
  126. // printf("\n[+] Download Begin ...\n");

  127.    }
  128.    else{
  129.        printf("\n[-] Bad request.\n");
  130.        exit(1);
  131.    }

  132. // 获得文件大小

  133.    
  134.    get_file_len=atoi(strstr(recv_data,"Content-Length: ")+16);
  135.    printf("\n[+] File Size: %ld\n",get_file_len);

  136. // 寻找真正的文件开始位置

  137.    
  138.    real_data_pos=strstr(recv_data,"\r\n\r\n")+4;
  139.    fputs(real_data_pos,fp);

  140. // 循环读取数据

  141.    
  142.    memset(recv_data,0,MAXLINE);
  143.    while((r_len=read(sock_fd,recv_data,MAXLINE))>0){
  144.        fputs(recv_data,fp);
  145. // printf("%d\n",r_len);

  146.        memset(recv_data,0,MAXLINE);
  147.    }
  148.    
  149.    fclose(fp);
  150.       
  151.    time(&t_e);
  152.    printf("\n[+] Download File OK at %s",ctime(&t_e)+4);
  153.    
  154. }

  155. DOWN abstract_host(char src[])
  156. {
  157.    DOWN file_download;
  158.    char *http="http://";
  159.    int port;
  160.    int port_flag=0;
  161.    int i,j=0,k,l=0,h;

  162.    i=strlen(http);
  163.    for(;i<strlen(src);i++){
  164.        if(src[i]==':'){
  165.            port_flag=1;
  166.            h=i-1;
  167.            i++;
  168.            for(;src[i]!='/';i++){ /* 提取ports */
  169.                file_download.ports[j++]=src[i];
  170.            }
  171.            file_download.ports[j]='\0';
  172.        }
  173.        if(src[i]=='/'){
  174.            k=i-1;
  175.            break;
  176.        }
  177.    }
  178.    
  179. /////////////////////////////////////////////////////////////////////


  180.    for(;i<strlen(src);i++){ /* 提取file */
  181.        file_download.file[l++]=src[i];
  182.    }
  183.    file_download.file[l]='\0';

  184. ////////////////////////////////////////////////////////////////////


  185.    i=strlen(http); /* 提取主机名 */
  186.    j=0;
  187.    k=((port_flag==1)?h:k);
  188.    
  189.    for(;i<=k;i++)
  190.        file_download.hosts[j++]=src[i];
  191.    file_download.hosts[j]='\0';
  192.    
  193. ////////////////////////////////////////////////////////////////////


  194.    if(port_flag==0) /* 提取port */
  195.        file_download.port=PORT;
  196.    else
  197.        file_download.port=atoi(file_download.ports);
  198.    
  199. ////////////////////////////////////////////////////////////////////

  200.    
  201.    return file_download;
  202. }

  203. int main(int argc,char **argv)
  204. {
  205.    if(argc<3){
  206.        usage(argv[0]);
  207.    }
  208.    
  209.    download_file(argv[1],argv[2]);
  210.       
  211.    return 0;
  212. }
复制代码

论坛徽章:
0
13 [报告]
发表于 2007-04-11 09:55 |只看该作者
其实看看rfc手册1626好象是这个.关于http的非常详细了.
请求行
头标
空行
请求内容

用socket_write方法请求
然后用socket_read获取
返回一般包括的就是
响应行
服务器头标
空行
响应数据了.
这个应该好实现吧.
任何语言实现起来都不难.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
14 [报告]
发表于 2007-04-12 08:37 |只看该作者
W.Z.T 写的不错,学习。

论坛徽章:
0
15 [报告]
发表于 2007-04-12 08:46 |只看该作者
搜索C HTTP协议.会有很多的.

论坛徽章:
0
16 [报告]
发表于 2012-09-07 14:58 |只看该作者
我最近也在弄这个东西,用原始套接字,按照http协议的要求,发起GET 然后接受web服务器发过来的内容,在解析一下就好了

论坛徽章:
0
17 [报告]
发表于 2012-09-07 15:10 |只看该作者
用c做网页下载和分析是**打蚊子。必须用python,ruby这样的语言啊

论坛徽章:
0
18 [报告]
发表于 2012-09-08 19:31 |只看该作者
通过套接字 send 发送 GET 请求报文  recv 接收 源码
或者用  WinAPI   UrlDownload下载   

论坛徽章:
0
19 [报告]
发表于 2012-09-08 22:18 |只看该作者
这里有个服务器和客户端还是什么的http协议的例子。。要找http协议请google http protocol。
https://github.com/justmao945/toy/tree/master/http-ev

http://www.w3.org/Protocols/这个我觉得不太容易看懂。。。应该有容易懂的文档的。。

论坛徽章:
0
20 [报告]
发表于 2012-09-23 21:08 |只看该作者
wget 不支持断点续传
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP