CheckHttpsUtils.java
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.*;
/**
* 测试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();
Collection<String> distinctUrl = getUrlList(urls);
OkHttpClient client = new OkHttpClient();
for (String url : distinctUrl) {
try {
//检测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);
}
}
}
/**
* 抽取域名, 并去重
*/
private static Collection<String> getUrlList(List<String> urls) {
Set<String> distinctUrl = new HashSet<>(); //去重
for (String url : urls) {
//去前后空格
url = StringUtils.trimToNull(url);
if (url == null) continue;
//将http强转为https
int i = url.indexOf("/", 10);
if (i > 0) {
url = url.substring(0, i);
}
url = url.toLowerCase();
url = url.replace("http://", "https://");
distinctUrl.add(url);
}
return distinctUrl;
}
}