Java 測試URL地址能否能正常銜接的代碼。本站提示廣大學習愛好者:(Java 測試URL地址能否能正常銜接的代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java 測試URL地址能否能正常銜接的代碼正文
Java 測試URL地址能否能正常銜接
public static int testWsdlConnection(String address) throws Exception { int status = 404; try { URL urlObj = new URL(address); HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection(); oc.setUseCaches(false); oc.setConnectTimeout(3000); // 設置超不時間 status = oc.getResponseCode();// 要求狀況 if (200 == status) { // 200是要求地址順遂連通。。 return status; } } catch (Exception e) { e.printStackTrace(); throw e; } return status; }
界說了一個int型,假如前往可200則是地址能勝利連通,假如前往0或許其他則是掉敗。
上面再看一段關於Java檢測URL能否可用或許可翻開的代碼
package test; import java.net.HttpURLConnection; import java.net.URL; import org.apache.log4j.Logger; public class CheskUrl { private static Logger logger = Logger.getLogger(CheskUrl.class); private static URL urlStr; private static HttpURLConnection connection; private static int state = -1; private static String succ; public synchronized String isConnect(String url) { int counts = 0; succ = null; if (url == null || url.length() <= 0) { return succ; } while (counts < 5) { try { urlStr = new URL(url); connection = (HttpURLConnection) urlStr.openConnection(); state = connection.getResponseCode(); if (state == 200) { succ = connection.getURL().toString(); } break; } catch (Exception ex) { counts++; logger.info("loop :" + counts); continue; } } return succ; } }