java测试URL是否连通的方法

来源:互联网 发布:恋恋有词软件 编辑:程序博客网 时间:2024/05/17 06:19
private boolean canGetHtmlCode(String httpUrl) {
  String htmlCode = "";
  try {
   InputStream in;
   URL url = new java.net.URL(httpUrl);
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection = (HttpURLConnection) url.openConnection();
   connection.setRequestProperty("User-Agent", "Mozilla/4.0");
   connection.connect();
   in = connection.getInputStream();
   byte[] buffer = new byte[50];
   in.read(buffer);
   htmlCode = new String(buffer);
  } catch (Exception e) {
  }
  if (RmStringHelper.empty(htmlCode) || htmlCode.equals("")) {
           return false;
  }
  return true;
 }
0 0
原创粉丝点击