|
|
package com.check;
|
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
import okhttp3.Request;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* 测试https是否可以访问
|
|
|
*
|
|
|
* @author weishaolei
|
|
|
*/
|
|
|
public class CheckHttpsUtils {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
//读取文件里的url
|
|
|
URL resource = CheckHttpsUtils.class.getClassLoader().getResource("url.txt");
|
|
|
List<String> urls = FileUtils.readLines(new File(resource.getFile()), "utf-8");
|
|
|
|
|
|
List<String> errorList = new ArrayList();
|
|
|
Set<String> distinctUrl = new HashSet<>(urls); //去重
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
for (String url : distinctUrl) {
|
|
|
try {
|
|
|
//去前后空格
|
|
|
String s = StringUtils.trimToNull(url);
|
|
|
if (s == null) continue;
|
|
|
//将http强转为https
|
|
|
url = url.replace("http://", "https://");
|
|
|
url = url.replace("HTTP://", "https://");
|
|
|
//检测https的GET请求
|
|
|
Request request = new Request.Builder()
|
|
|
.url(url)
|
|
|
.get()
|
|
|
.build();
|
|
|
client.newCall(request).execute();
|
|
|
System.out.println(url + " 成功支持HTTPS");
|
|
|
} catch (Exception e) {
|
|
|
System.out.println(url + " 不支持HTTPS, 错误原因: " + e.getMessage());
|
|
|
errorList.add(url);
|
|
|
}
|
|
|
}
|
|
|
if (errorList.isEmpty()) {
|
|
|
System.out.println("所有域名都支持https.");
|
|
|
} else {
|
|
|
System.err.println("--------------------------------------------------------");
|
|
|
System.err.println("--------------- 无法访问https的所有域名如下 ----------------");
|
|
|
System.err.println("--------------------------------------------------------");
|
|
|
for (String s : errorList) {
|
|
|
System.err.println(s);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|