I have a wee program that simply reads from a text file containing LOTS of URLs (1,975 to be exact) in order to test this, I created a much smaller file with only 3 URLs in it. I have been able to capture THAT a redirect is happening (with connection.setInstanceFollowRedirects(false);
) and it works perfectly and returns the 302 that I expected to see.
What I am trying to see is the destination URL that it successfully re-directs TO How can I do this? I have just edited to try some other things I saw on here but it is not returning the redirected URL, just the same original URL pre-redirect.....
Here is what I have for your viewing pleasure
(please forgive any glaring ignorance you see, I am still quite new to JAVA):
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.charset.*;
import java.net.*;
//import java.security.cert.*;
public class URL_Tester {
Charset charset = Charset.forName("US-ASCII");
//Path file = Paths.get("url_list_small.txt"); // COMMENT OUT THIS LINE, AND UNCOMMENT OUT THE NEXT LINE TO SEE IT WORK
Path file = Paths.get("url_list_working.txt");
private BufferedReader reader;
//private BufferedWriter writer;
private ArrayList<String> urls;
private URL url;
public static void main(String[] args) {
URL_Tester tester = new URL_Tester();
tester.getArry();
//tester.printArry();
tester.getConnected();
} // end of main METHOD
/*
* The Method Below simply reads from a text file, and populates an ArrayList of Strings
* The text file is meant to contain URLs for testing
*/
public void getArry() {
urls = new ArrayList<String>();
String line = null;
//System.out.println(file.toString());
try {
reader = Files.newBufferedReader(file, charset);
while ((line = reader.readLine()) != null) {
urls.add(line);
}
}
catch (IOException ex) {
System.out.println(ex);
}
} // end of getArry METHOD
/*
* The Method below simply prints out the contents of the ArrayLlist to the console (for testing purposes)
*/
public void printArry() {
System.out.println(" ");
for (int i = 0; i < urls.size(); i++) {
System.out.println(urls.get(i));
}
} // end printArry METHOD
/*
* The Method below loops through the ArrayList of URL strings it attempts to make HTTP connection to each URL provided
*/
public void getConnected() {
int code;
for (int i = 0; i < urls.size(); i++) {
try {
url = new URL(urls.get(i));
}
catch (MalformedURLException ex) {
System.out.println("\n*********** MalformedURLException area ********************");
ex.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
//connection.connect(); // AT THIS LINE AN IOException IS THROWN
//sample code begins here
System.out.println( "orignal url: " + connection.getURL() );
connection.connect();
System.out.println( "connected url: " + connection.getURL() );
InputStream is = connection.getInputStream();
System.out.println( "redirected url: " + connection.getURL() );
is.close();
// sample code ends here
code = connection.getResponseCode();
System.out.println(urls.get(i) + " code == " + code);
}
catch (Exception ix) {
System.out.println("\n*************** IOException area ************************");
ix.printStackTrace();
break;
}
} // end LOOP
} // end getConnected METHOD
} // end CLASS