我有一个简单的程序只是从包含大量URL(准确地说是1,975)的文本文件中读取,为了测试它,我创建了一个小得多的文件,其中只有3个URL。我已经能够捕获重定向正在发生(使用connection.setInstanceFollowRedirects(false);
)并且它完美地工作并返回我期望看到的302.
我想看到的是它成功重定向的目标网址 我该怎么做?我刚刚编辑过来尝试我在这里看到的其他一些东西,但它没有返回重定向的URL,只是相同的原始URL预重定向.....
这是我的观赏乐趣
(请原谅你看到的任何明显的无知,我仍然是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