亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android檢測(cè)url地址是否可達(dá)的兩種方法

 更新時(shí)間:2019年01月31日 10:42:38   作者:JJdary  
今天小編就為大家分享一篇Android檢測(cè)url地址是否可達(dá)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

方法一

try{
  URL url = new URL(address);
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  conn.setUseCaches(false);
  conn.setInstanceFollowRedirects(true);
  conn.setConnectTimeout(waitMilliSecond);
  conn.setReadTimeout(waitMilliSecond);

  //HTTP connect
  try {
    conn.connect();
  } catch(Exception e) {
    e.printStackTrace();
    return false;
  }

  int code = conn.getResponseCode();
  if ((code >= 100) && (code < 400)){
    return true;
  }

  return false;
}catch (Exception e){
  e.printStackTrace();
  return false;
}

方法二

try {
  URL url = new URL(address);
  String host = url.getHost();
  int port = url.getPort();
  if (port == -1) {
    port = 80;
  }
  Socket socket = new Socket();
  InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName(host), port);

  socket.connect(isa, timeout);
  if (socket.isConnected()) {
    return true;
  } else {
    return false;
  }
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (socket != null) {
    try {
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

以上這篇Android檢測(cè)url地址是否可達(dá)的兩種方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論