#java #ssl #sslhandshakeexception
#java #ssl #исключение sslhandshakeexception
Вопрос:
Я написал код, который извлекает код состояния сайтов, но когда я подключился к VPN, сайт, возвращенный удаленным хостом, прекратил рукопожатие без VPN, ошибки нет, он возвращает 200, как и другие сайты. У вас есть какие-либо идеи? Ниже приведен общий код
public class Main {
public static void checkSites() {
try {
File links = new File("./linkler.txt");
Scanner scan = new Scanner(links);
ArrayList<String> list = new ArrayList<>();
while (scan.hasNext()) {
list.add(scan.nextLine());
}
File linkStatus = new File("LinkStatus.txt");
if (!linkStatus.exists()) {
linkStatus.createNewFile();
} else {
System.out.println("File already exists");
}
BufferedWriter writer = new BufferedWriter(new FileWriter(linkStatus));
for (String link : list) {
try {
if (!link.startsWith("http")) {
link = "http://" link;
}
URL url = new URL(link);
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("HEAD");
http.setConnectTimeout(10000);
http.setReadTimeout(10000);
int statusCode = http.getResponseCode();
if (statusCode == 403) { // If HTTP status code returns 403, this block of code will check the site 3 times. It makes sleep to thread 10 seconds, 15 seconds and 100 seconds stepwise.
Thread.sleep(10000);
int tryAgainLater = 1;
while (tryAgainLater <= 3) {
try {
if (!link.startsWith("http")) {
link = "http://" link;
}
url = new URL(link);
HttpURLConnection.setFollowRedirects(true);
http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("HEAD");
http.setConnectTimeout(5000);
http.setReadTimeout(8000);
http.disconnect();
System.out.println(link " " statusCode);
writer.write(link " " statusCode);
writer.newLine();
} catch (Exception e) {
writer.write(link " " e.getMessage());
writer.newLine();
System.out.println(link " " e.getMessage());
}
if (tryAgainLater == 1) {
System.out.println("Second time program tries to get response code");
Thread.sleep(15000);
}
if (tryAgainLater == 2) {
System.out.println("Third time program tries to get response code");
Thread.sleep(100000);
}
if (tryAgainLater == 3) {
System.out.println("403 HTTP code hasn't broken.");
}
tryAgainLater;
}
}
http.disconnect(); // It helps to increasing speed of code.
System.out.println(link " " statusCode);
writer.write(link " " statusCode);
writer.newLine();
} catch (Exception e) {
writer.write(link " " e.getMessage());
writer.newLine();
System.out.println(link " " e.getMessage());
}
}
try {
writer.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
Timer myTimer = new Timer();
TimerTask sendingRequest = new TimerTask() {
int loopTeller = 1;
public void run() {
checkSites();
System.out.println("Loop has ended " loopTeller " time(s).");
loopTeller ;
}
};
myTimer.schedule(sendingRequest, 0, 20000);
}
Я проверил вопросы, связанные с этой темой, но эти ответы не смогли решить мою проблему.