Java如何判斷一個(gè)url是否有效
Java如何判斷一個(gè)url是否有效
Java判斷一個(gè)url是否有效,代碼如下所示:
import java.io.InputStream;
import java.net.URL;
public class URLTest {
public static boolean isUrlValid(String strLink) {
URL url;
try {
url = new URL(strLink);
HttpURLConnection connt = (HttpURLConnection)url.openConnection();
//也可以通過(guò)判斷code碼判斷是否有效
//一般是返回200 但是不保證有些網(wǎng)站請(qǐng)求成功返回的不是200
int responseCode = connt .getResponseCode();
if(200 == responseCode){
//鏈接有效
}else{
//鏈接無(wú)效
}
connt.setRequestMethod("HEAD");
String strMessage = connt.getResponseMessage();
if (strMessage.compareTo("Not Found") == 0) {
return false;
}
connt.disconnect();
} catch (Exception e) {
return false;
}
return true;
}
}可以使用Java中的URL類(lèi)來(lái)判斷URL是否有效。URL類(lèi)提供了一個(gè)openConnection()方法,可以打開(kāi)一個(gè)連接并返回一個(gè)URLConnection對(duì)象,該對(duì)象可以用來(lái)檢查連接是否有效。
以下是一個(gè)示例代碼:
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class URLValidator {
public static void main(String[] args) {
String url = "https://www.google.com";
boolean isValid = isValidURL(url);
System.out.println("Is " + url + " valid? " + isValid);
}
public static boolean isValidURL(String url) {
try {
URLConnection conn = new URL(url).openConnection();
conn.connect();
return true;
} catch (IOException e) {
return false;
}
}
}該代碼將嘗試打開(kāi)指定的URL并檢查是否有效。如果URL有效,則openConnection()方法將成功打開(kāi)連接并返回URLConnection對(duì)象。如果URL無(wú)效,則會(huì)拋出IOException并返回false。
java 判斷一個(gè)url是否可以訪問(wèn)的方法
有些時(shí)候,我們需要判斷某個(gè)url是否可以訪問(wèn),可以訪問(wèn)了,才允許繼續(xù)進(jìn)行,目前有兩種方式,最后使用帶超時(shí)時(shí)間的,
因?yàn)榈谝环N超時(shí)時(shí)間不定,可能會(huì)出現(xiàn)阻塞的情況。
package com.url;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class TestUrl {
public static void main(String[] args) {
testUrl("http://1.3.3.3/test");
//最好使用下面這個(gè),上面那個(gè)超時(shí)時(shí)間不定,所以可能會(huì)導(dǎo)致卡住的情況
testUrlWithTimeOut("http://1.3.3.3", 2000);
}
public static void testUrl(String urlString){
long lo = System.currentTimeMillis();
URL url;
try {
url = new URL(urlString);
InputStream in = url.openStream();
System.out.println("連接可用");
} catch (Exception e1) {
System.out.println("連接打不開(kāi)!");
url = null;
}
System.out.println(System.currentTimeMillis()-lo);
}
public static void testUrlWithTimeOut(String urlString,int timeOutMillSeconds){
long lo = System.currentTimeMillis();
URL url;
try {
url = new URL(urlString);
URLConnection co = url.openConnection();
co.setConnectTimeout(timeOutMillSeconds);
co.connect();
System.out.println("連接可用");
} catch (Exception e1) {
System.out.println("連接打不開(kāi)!");
url = null;
}
System.out.println(System.currentTimeMillis()-lo);
}
}到此這篇關(guān)于Java判斷一個(gè)url是否有效的文章就介紹到這了,更多相關(guān)java url是否有效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot在啟動(dòng)類(lèi)main方法中調(diào)用service層方法報(bào)“空指針異?!暗慕鉀Q辦法
這篇文章主要介紹了SpringBoot在啟動(dòng)類(lèi)main方法中調(diào)用service層方法報(bào)“空指針異?!暗慕鉀Q辦法,大多數(shù)情況下,我們使用Springboot是創(chuàng)建一個(gè)maven項(xiàng)目,然后通過(guò)controller層的接口調(diào)用,但也有特殊情況,文章介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
Redis 集成Spring的示例代碼(spring-data-redis)
本篇文章主要介紹了Redis 集成Spring的示例代碼(spring-data-redis) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒(méi)有src目錄問(wèn)題
這篇文章主要介紹了解決 IDEA 創(chuàng)建 Gradle 項(xiàng)目沒(méi)有src目錄問(wèn)題,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Spring中的@EnableScheduling定時(shí)任務(wù)注解
這篇文章主要介紹了Spring中的@EnableScheduling注解,@EnableScheduling是 Spring Framework 提供的一個(gè)注解,用于啟用 Spring 的定時(shí)任務(wù)功能,通過(guò)使用這個(gè)注解,可以在 Spring 應(yīng)用程序中創(chuàng)建定時(shí)任務(wù),需要的朋友可以參考下2024-01-01
淺析java并發(fā)中的Synchronized關(guān)鍵詞
這篇文章主要介紹了java并發(fā)中的Synchronized關(guān)鍵詞,本文通過(guò)思路代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02

