使用带有GET的java.net.URLConnection,如何获取重定向URL?

时间:2018-03-26 12:35:19

标签: java http http-get urlconnection

使用java.net.URLConnection,在特定URL上使用GET,此特定URL将重定向到新页面。如何从响应中获取新URL?

1 个答案:

答案 0 :(得分:0)

鉴于Rishal的链接和amobiz的回答是我正在寻找的:

URLConnection con = new URL( url ).openConnection();
System.out.println( "orignal url: " + con.getURL() );
con.connect();
System.out.println( "connected url: " + con.getURL() );
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
is.close();