Get url after redirect?
I'm using URLConnection to connect to websites. I've got an issue where I follow a URL and get redirected to a new page. I can get the HTML of this page but I need to be able to get the URL of this new page. I tried-
returnURL comes back with the URL I opened. But I visit the url in my browser and get redirected to a new site. So the URL coming back is incorrect. How can I get the redirected URL?
url = new URL(u);
connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:126.96.36.199) Gecko/20100316 Firefox/3.6.2");
url2 = connection.getURL();
returnURL = url2.getHost() + url2.getPath();
Did you try searching the net? I found a number of solutions in just the first page of search results.
Yah I did and I didn't find anything. But I've got it figured out.
Can you post your solution so the next OP can find it?