Searched for a bit on the internet and from few websites gather and compiled this bit of code which reads domains from csv, checks redirection and then writes result in a csv,
public static void main(String[] args) {
try {
FileReader filereader = new FileReader(System.getProperty("user.dir") + "//data/file.csv");
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).withSkipLines(1).build();
List<String[]> allData = csvReader.readAll();
List<String[]> data = new ArrayList<String[]>();
URL url;
int count = 0;
for (String[] row : allData) {
for (String cell : row) {
int responseCode = 0;
String Location = null;
url = new URL("http://" + cell);
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
responseCode = huc.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
Location = huc.getHeaderField("Location");
}
if (Location != null && Location.startsWith("/")) {
Location = url.getProtocol() + "://" + url.getHost() + Location;
}
} catch (Exception e) {
e.printStackTrace();
}
count++;
if (Location != null && Location.startsWith("https")) {
data.add(new String[] { Integer.toString(count), String.valueOf(url), Integer.toString(responseCode), Location, "Pass" });
} else {
data.add(new String[] { Integer.toString(count), String.valueOf(url), Integer.toString(responseCode), Location, "Fail" });
}
}
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String currentDate = df.format(new Date());
File file = new File(System.getProperty("user.dir") + "//data/report-" + currentDate + ".csv");
FileWriter outputFile = new FileWriter(file);
CSVWriter writer = new CSVWriter(outputFile, ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
String[] header = { "#", "URL", "Response Code", "Redirected To", "Status" };
writer.writeNext(header);
writer.writeAll(data);
writer.close();
} catch (
Exception e) {
e.printStackTrace();
}
}