100分求 :如何检测一个URL是否可用??

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

url可能是page也可能是文件,图片,程序需要检测此url是否可以访问,谢谢。。。  
  另外,当url比较多的时候,如何快速检测各个url的状态呢?

· 网友精彩回答:

发表者:vicky_jam

一个一个试啦~  
   
  http   头   返回200   说明可以用~  
  然后再   得到   http   头返回的文件类型~~

发表者:goodboyws

ping或者说icmp协议,给你一段代码  
    //   module   name:   ping.c  
  //  
  //   description:  
  //   this   sample   illustrates   how   an   icmp   ping   app   can   be   written  
  //   using   the   sock_raw   socket   type   and   ipproto_icmp   protocol.  
  //   by   creating   a   raw   socket,   the   underlying   layer   does   not   change  
  //   the   protocol   header   so   that   when   we   submit   the   icmp   header  
  //   nothing   is   changed   so   that   the   receiving   end   will   see   an    
  //   icmp   packet.   additionally,   we   use   the   record   route   ip   option  
  //   to   get   a   round   trip   path   to   the   endpoint.   note   that   the   size  
  //   of   the   ip   option   header   that   records   the   route   is   limited   to  
  //   nine   ip   addresses.  
  //  
  //   compile:  
  //   cl   -o   ping   ping.c   ws2_32.lib   /zp1  
  //  
  //   command   line   options/parameters:  
  //   ping   [host]   [packet-size]  
  //    
  //   host   string   name   of   host   to   ping  
  //   packet-size   integer   size   of   packet   to   send    
  //   (smaller   than   1024   bytes)  
  //  
  //#pragma   pack(1)  
   
  #define   win32_lean_and_mean  
  #include    
  #include    
  #include    
  #include    
   
  #define   ip_record_route   0x7  
  //    
  //   ip   header   structure  
  //  
  typedef   struct   _iphdr    
  {  
  unsigned   int   h_len:4;   //   length   of   the   header  
  unsigned   int   version:4;   //   version   of   ip  
  unsigned   char   tos;   //   type   of   service  
  unsigned   short   total_len;   //   total   length   of   the   packet  
  unsigned   short   ident;   //   unique   identifier  
  unsigned   short   frag_and_flags;   //   flags  
  unsigned   char   ttl;   //   time   to   live  
  unsigned   char   proto;   //   protocol   (tcp,   udp   etc)  
  unsigned   short   checksum;   //   ip   checksum  
   
  unsigned   int   sourceip;  
  unsigned   int   destip;  
  }   ipheader;  
  #define   icmp_echo   8  
  #define   icmp_echoreply   0  
  #define   icmp_min   8   //   minimum   8-byte   icmp   packet   (header)  
   
  //  
  //   icmp   header   structure  
  //  
  typedef   struct   _icmphdr    
  {  
  byte   i_type;  
  byte   i_code;   //   type   sub   code  
  ushort   i_cksum;  
  ushort   i_id;  
  ushort   i_seq;  
  //   this   is   not   the   standard   header,   but   we   reserve   space   for   time  
  ulong   timestamp;  
  }   icmpheader;  
   
  //  
  //   ip   option   header   -   use   with   socket   option   ip_options  
  //  
  typedef   struct   _ipoptionhdr  
  {  
  unsigned   char   code;   //   option   type  
  unsigned   char   len;   //   length   of   option   hdr  
  unsigned   char   ptr;   //   offset   into   options  
  unsigned   long   addr[9];   //   list   of   ip   addrs  
  }   ipoptionheader;  
   
  #define   def_packet_size   32   //   default   packet   size  
  #define   max_packet   1024   //   max   icmp   packet   size  
  #define   max_ip_hdr_size   60   //   max   ip   header   size   w/options  
   
  bool   brecordroute;  
  int   datasize;  
  char   *lpdest;  
   
  //  
  //   function:   usage  
  //  
  //   description:  
  //   print   usage   information  
  //  
  void   usage(char   *progname)  
  {  
  printf("usage:   ping   -r   [data   size]\n");  
  printf("   -r   record   route\n");  
  printf("   host   remote   machine   to   ping\n");  
  printf("   datasize   can   be   up   to   1kb\n");  
  exitprocess(-1);  
   
  //    
  //   function:   fillicmpdata  
  //  
  //   description:  
  //   helper   function   to   fill   in   various   fields   for   our   icmp   request  
  //  
  void   fillicmpdata(char   *icmp_data,   int   datasize)  
  {  
  icmpheader   *icmp_hdr   =   null;  
  char   *datapart   =   null;  
   
  icmp_hdr   =   (icmpheader*)icmp_data;  
  icmp_hdr->i_type   =   icmp_echo;   //   request   an   icmp   echo  
  icmp_hdr->i_code   =   0;  
  icmp_hdr->i_id   =   (ushort)getcurrentprocessid();  
  icmp_hdr->i_cksum   =   0;  
  icmp_hdr->i_seq   =   0;  
   
  datapart   =   icmp_data   +   sizeof(icmpheader);  
  //  
  //   place   some   junk   in   the   buffer  
  //  
  memset(datapart,e,   datasize   -   sizeof(icmpheader));  
  }  
   
  //    
  //   function:   checksum  
  //  
  //   description:  
  //   this   function   calculates   the   16-bit   ones   complement   sum  
  //   of   the   supplied   buffer   (icmp)   header  
  //  
  ushort   checksum(ushort   *buffer,   int   size)    
  {  
  unsigned   long   cksum=0;  
   
  while   (size   >   1)    
  {  
  cksum   +=   *buffer++;  
  size   -=   sizeof(ushort);  
  }  
  if   (size)    
  {  
  cksum   +=   *(uchar*)buffer;  
  }  
  cksum   =   (cksum   >>   16)   +   (cksum   &   0xffff);  
  cksum   +=   (cksum   >>16);  
  return   (ushort)(~cksum);  
  //  
  //   function:   decodeipoptions  
  //  
  //   description:  
  //   if   the   ip   option   header   is   present,   find   the   ip   options  
  //   within   the   ip   header   and   print   the   record   route   option  
  //   values  
  //  
  void   decodeipoptions(char   *buf,   int   bytes)  
  {  
  ipoptionheader   *ipopt   =   null;  
  in_addr   inaddr;  
  int   i;  
  hostent   *host   =   null;  
   
  ipopt   =   (ipoptionheader   *)(buf   +   20);  
   
  printf("rr:   ");  
  for(i   =   0;   i   <   (ipopt->ptr   /   4)   -   1;   i++)  
  {  
  inaddr.s_un.s_addr   =   ipopt->addr[i];  
  if   (i   !=   0)  
  printf("   ");  
  host   =   gethostbyaddr((char   *)&inaddr.s_un.s_addr,  
  sizeof(inaddr.s_un.s_addr),   af_inet);  
  if   (host)  
  printf("(%-15s)   %s\n",   inet_ntoa(inaddr),   host->h_name);  
  else  
  printf("(%-15s)\n",   inet_ntoa(inaddr));  
  }  
  return;  
  }  
   
  //  
  //   function:   decodeicmpheader  
  //  
  //   description:  
  //   the   response   is   an   ip   packet.   we   must   decode   the   ip   header   to  
  //   locate   the   icmp   data.  
  //  
  void   decodeicmpheader(char   *buf,   int   bytes,    
  struct   sockaddr_in   *from)  
  {  
  ipheader   *iphdr   =   null;  
  icmpheader   *icmphdr   =   null;  
  unsigned   short   iphdrlen;  
  dword   tick;  
  static   int   icmpcount   =   0;  
   
  iphdr   =   (ipheader   *)buf;  
  //   number   of   32-bit   words   *   4   =   bytes  
  iphdrlen   =   iphdr->h_len   *   4;  
  tick   =   gettickcount();  
   
  if   ((iphdrlen   ==   max_ip_hdr_size)   &&   (!icmpcount))  
  decodeipoptions(buf,   bytes);  
   
  if   (bytes   <   iphdrlen   +   icmp_min)    
  {  
  printf("too   few   bytes   from   %s\n",    
  inet_ntoa(from->sin_addr));  
  }  
  icmphdr   =   (icmpheader*)(buf   +   iphdrlen);  
   
  if   (icmphdr->i_type   !=   icmp_echoreply)    
  {  
  printf("nonecho   type   %d   recvd\n",   icmphdr->i_type);  
  return;  
  }  
  //   make   sure   this   is   an   icmp   reply   to   something   we   sent!  
  //  
  if   (icmphdr->i_id   !=   (ushort)getcurrentprocessid())    
  {  
  printf("someone   elses   packet!\n");  
  return   ;  
  }  
  printf("%d   bytes   from   %s:",   bytes,   inet_ntoa(from->sin_addr));  
  printf("   icmp_seq   =   %d.   ",   icmphdr->i_seq);  
  printf("   time:   %d   ms",   tick   -   icmphdr->timestamp);  
  printf("\n");  
   
  icmpcount++;  
  return;  
  }  
   
  void   validateargs(int   argc,   char   **argv)  
  {  
  int   i;  
   
  brecordroute   =   false;  
  lpdest   =   null;  
  datasize   =   def_packet_size;  
   
  for(i   =   1;   i   <   argc;   i++)  
  {  
  if   ((argv[i][0]   ==   -)   ||   (argv[i][0]   ==   /))  
  {  
  switch   (tolower(argv[i][1]))  
  {  
  case   r:   //   record   route   option  
  brecordroute   =   true;  
  break;  
  default:  
  usage(argv[0]);  
  break;  
  }  
  }  
  else   if   (isdigit(argv[i][0]))  
  datasize   =   atoi(argv[i]);  
  else  
  lpdest   =   argv[i];  
  }  
  }  
  //  
  //   function:   main  
  //  
  //   description:  
  //   setup   the   icmp   raw   socket,   and   create   the   icmp   header.   add  
  //   the   appropriate   ip   option   header,   and   start   sending   icmp  
  //   echo   requests   to   the   endpoint.   for   each   send   and   receive,  
  //   we   set   a   timeout   value   so   that   we   dont   wait   forever   for   a    
  //   response   in   case   the   endpoint   is   not   responding.   when   we  
  //   receive   a   packet   decode   it.  
  //  
  int   main(int   argc,   char   **argv)  
  {  
   
  wsadata   wsadata;  
  socket   sockraw   =   invalid_socket;  
  struct   sockaddr_in   dest,  
  from;  
  int   bread,  
  fromlen   =   sizeof(from),  
  timeout   =   1000,  
  ret;  
  char   *icmp_data   =   null,  
  *recvbuf   =   null;  
  unsigned   int   addr   =   0;  
  ushort   seq_no   =   0;  
  struct   hostent   *hp   =   null;  
  ipoptionheader   ipopt;  
   
   
 

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved