update了UrlCheck工具类

This commit is contained in:
sjm
2023-12-23 15:34:05 +08:00
parent 32ec34633c
commit 1b4bf28371
11 changed files with 100 additions and 25 deletions

View File

@@ -0,0 +1,74 @@
package com.lovenav.utils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import sun.net.www.protocol.http.HttpURLConnection;
import java.io.IOException;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
public class UrlCheckUtil {
/**
* 判断链接是否有效
* 输入链接
* 返回true或者false
*/
public static boolean isUrlValid(String strLink) {
URL url;
try {
url = new URL(strLink);
HttpURLConnection connt = (HttpURLConnection)url.openConnection();
connt.setRequestMethod("HEAD");
String strMessage = connt.getResponseMessage();
if (strMessage.compareTo("Not Found") == 0) {
return false;
}
connt.disconnect();
} catch (Exception e) {
return false;
}
return true;
}
public static String checkUrlConnection(String url) {
// 创建http POST请求
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(600)// 设置连接主机服务超时时间
.setConnectionRequestTimeout(1000)// 设置连接请求超时时间
.setSocketTimeout(1000)// 设置读取数据连接超时时间
.build();
// 为httpPost实例设置配置
httpGet.setConfig(requestConfig);
// 设置请求头
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
int statusCode = 404;
try {
httpclient = HttpClients.createDefault();// 创建Httpclient对象
response = httpclient.execute(httpGet);// 执行请求
statusCode = response.getStatusLine().getStatusCode();
}catch (SocketException e) {
return "404";
} catch (IOException e) {
System.out.println("报错");
return "404";
}
return String.valueOf(statusCode);
}
public static boolean CheckHttp(String address) throws URISyntaxException, MalformedURLException {
URL url = new URL(address);
URI uri = url.toURI();
System.out.println(uri.getScheme());
if(uri.getScheme().equals("https")){
return true;
}else
return false;
}
}