Created
November 26, 2019 13:36
-
-
Save trykopa/771a12cb794b716e490cec1abba90abf to your computer and use it in GitHub Desktop.
HomeWork 12 Task 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package HomeWork12; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class CheckURLList { | |
public static void main(String[] args) throws IOException { | |
String file = "/Users/at/eclipse-workspace/oop/HomeWork12/urllist.txt"; | |
List<String> list = getListFromFileStream(file); | |
list.forEach(System.out::println); | |
checkURLlist(list); | |
} | |
public static List<String> getListFromFileStream(String file) { | |
List<String> listStream = new ArrayList<>(); | |
try (Stream<String> stream = Files.lines(Paths.get(file))) { | |
listStream = stream.collect(Collectors.toList()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return listStream; | |
} | |
public static void checkURLlist(List<String> list) throws IOException { | |
for (String url : list) { | |
if (checkURL(url)) { | |
System.out.format("%s - site is now available %n", url); | |
} else { | |
String tmpurl = url.replace("http", "https"); | |
if (checkURL(tmpurl)) { | |
System.out.format("%s - site is now available %n", tmpurl); | |
} else { | |
System.out.format("%s - site is currently unavailable %n", url); | |
} | |
} | |
} | |
} | |
public static boolean checkURL(String url) throws IOException { | |
boolean result = true; | |
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); | |
connection.setRequestMethod("HEAD"); | |
connection.setConnectTimeout(10000); | |
int responseCode = connection.getResponseCode(); | |
if (responseCode != 200) { | |
result = false; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment