当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java判断一个链接是否有效
发布时间:2010/8/1 11:55:43 来源:城市学习网 编辑:ziteng
  一、
  /**
  * 判断链接是否有效
  * 输入链接
  * 返回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) {
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved