(转载)基于Socket与C的WEB页面抓取程序

 基于C与Socket编写了一个HTTP页面检查程序,通过这个程序,可以加载指定WEB服务器上的页面信息,这在爬虫、抓取WEB页面、分析是否有挂马行为有参考意义。

  代码如下:

  1. <span xmlns="http://www.w3.org/1999/xhtml" style="">//---  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <iostream.h>  
  6. #include <winsock2.h>  
  7. #include<time.h>  
  8.   
  9. char *checkhttp(char *path)  
  10. {  
  11.     WSADATA ds_Data;  
  12.     SOCKET ds_ClientSocket ;  
  13.     struct sockaddr_in ds_ServerAddr ;  
  14.     char recvbuf[1000000];  
  15.       
  16.     if(WSAStartup(MAKEWORD(2,2),&ds_Data)!=0)  
  17.     {  
  18.         printf("无法加载套接字协议栈\n");  
  19.         return NULL;  
  20.     }  
  21.   
  22.     ds_ServerAddr.sin_family = AF_INET ;  
  23.     ds_ServerAddr.sin_port = htons(80) ;  
  24.   
  25.     if(path == NULL)  
  26.         return NULL;  
  27.   
  28.     ds_ServerAddr.sin_addr.s_addr = inet_addr( path ) ;  
  29.     ds_ClientSocket = socket(AF_INET,SOCK_STREAM,0) ;  
  30.       
  31.     if(ds_ClientSocket==INVALID_SOCKET)  
  32.     {  
  33.         printf("套接字初始化出现错误;错误号:%d\n",WSAGetLastError());  
  34.         return NULL;  
  35.     }  
  36.     if( connect(ds_ClientSocket,(struct sockaddr *)&ds_ServerAddr,sizeof( ds_ServerAddr ) )==INVALID_SOCKET)  
  37.     {  
  38.        
  39.         printf("连接服务器出现错误;错误号:%d\n",WSAGetLastError());  
  40.         return NULL;  
  41.     }  
  42.     char buf[1023];  
  43.     strcpy(buf,"Get / HTTP 1.0\r\n");  
  44.     send( ds_ClientSocket,buf,strlen(buf),0 );  
  45.     printf("发送完毕");  
  46.     int iLen=recv( ds_ClientSocket,recvbuf,1000000,0 );  
  47.     if(iLen==SOCKET_ERROR || iLen <= 0 ||iLen != (int )strlen(recvbuf))  
  48.     {  
  49.         printf("出现失误");  
  50.         return NULL;  
  51.     }  
  52.     printf("%s",recvbuf);  
  53.         return recvbuf;  
  54. }  
  55. void main()  
  56. {  
  57.     
  58.    checkhttp("172.16.1.1");  
  59. }  
  60. </span>  

 

 

posted @ 2013-09-01 11:18  苏苏zhao  阅读(152)  评论(0编辑  收藏  举报