41 lines
1.4 KiB
Java
41 lines
1.4 KiB
Java
package io.gitlab.jfronny.resclone.util;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
import io.gitlab.jfronny.resclone.Resclone;
|
|
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Scanner;
|
|
import java.util.Set;
|
|
|
|
public class UrlUtils {
|
|
|
|
public static boolean urlValid(String url) {
|
|
try {
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).toURI().toURL().openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.connect();
|
|
return connection.getResponseCode() == 200;
|
|
} catch (Throwable e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static String readStringFromURL(String requestURL) throws IOException {
|
|
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) {
|
|
scanner.useDelimiter("\\A");
|
|
return scanner.hasNext() ? scanner.next() : "";
|
|
}
|
|
}
|
|
|
|
public static <T> T readJsonFromURL(String requestUrl, Class<T> classOfT) throws IOException {
|
|
return Resclone.gson.fromJson(readStringFromURL(requestUrl), classOfT);
|
|
}
|
|
|
|
public static <T> Set<T> readJsonFromURLSet(String requestUrl, Class<T> classOfT) throws IOException {
|
|
return Resclone.gson.fromJson(readStringFromURL(requestUrl), TypeToken.getParameterized(Set.class, classOfT).getType());
|
|
}
|
|
|
|
} |