设为首页 加入收藏

TOP

Java判断一个链接是否有效
2014-11-14 09:15:35 来源: 作者: 【 】 浏览:24
Tags:Java 判断 一个 链接 是否 有效

  一、


  /**


  * 判断链接是否有效


  * 输入链接


  * 返回true或者false


  */


  public static boolean isValid(String strLink) {


  URL url;


  try {


  url = new URL(strLink);


  HttpURLConnection connt = (HttpURLConnection)url.openConnection();


  connt.setRequestMethod("HEAD");


  String strMessage = connt.getResponseMessage();


  if (strMessage.compareTo("Not Found") == 0) {


  return false;


  }


  connt.disconnect();


  } catch (Exception e) {


  return false;


  }


  return true;


  }


  二、


  package test;


  import java.net.*;


  public class riqi {


  public static void main(String[] args) {


  try {


  URL url=new URL( "http://www.aaa.com ");


  URLConnection conn=url.openConnection();


  String str=conn.getHeaderField(0);


  if (str.indexOf( "OK ")> 0)


  {


  System.out.println( "正常! ");


  }else{


  System.out.println( "不能游览 ");


  }


  } catch (Exception ex) {


  }


  编辑特别推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇专为新手定制的Java学习计划建议 下一篇Java注释规范整理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: