0

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
Andrew B
  • 333
  • 3
  • 12
  • Are you missing something in the question? – User27854 Jan 30 '15 at 05:48
  • Superclass java.net.URLConnection has a getURL() method. – laune Jan 30 '15 at 05:53
  • 1
    The exact question has been asked before and there is a solution at http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url – ramp Jan 30 '15 at 05:54
  • @ramp I have seen that one and tried it's contents, with no success Also I have just added some clarification and code to this – Andrew B Jan 30 '15 at 05:58
  • @all I see, I used 'connection.getHeaderField("Location") and it worked! [link](http://stackoverflow.com/questions/14951696/java-urlconnection-get-the-final-redirected-url) – Andrew B Jan 30 '15 at 06:03
  • @Andrew, the post I had linked to has the exact same solution. Anyways good that you got it working. – ramp Jan 30 '15 at 06:06
  • @ramp Ah, it sure does. I apologize, I failed to read far enough down there – Andrew B Jan 30 '15 at 06:13

0 Answers0