Reputation: 30136
How can I get the "final location" (a.k.a. landing-page) of the following URL:
My code (below) takes this string as input.
The output should be something like 'http://weeklyad.target.com', but instead, I just get the same URL.
No need to mention, I am unable to solve this specific case, but I still need a general solution.
Here is my simple Java code, using HttpURLConnection (where String ref is the input):
HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection();
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent","");
if (con.getResponseCode()/100 == 3)
{
String target = con.getHeaderField("Location");
if (target != null)
return target;
}
return con.getURL().toString();
Does anybody have any idea what am I doing wrong?
Upvotes: 0
Views: 766
Reputation: 5598
The server returns this:
<html>
<head>
<meta http-equiv="refresh" content="1; url=http://weeklyad.target.com">
<title>Redirect</title>
<script language="javascript" type="text/javascript">
<!--
function track_click(url)
{
var req = new Image();
req.src = url;
}
function redirect(url)
{
window.location = url;
}
var url_raw = "http://weeklyad.target.com";
var url_enc = "http%3A%2F%2Fweeklyad.target.com";
track_click("http://track.pubmatic.com/AdServer/AdDisplayTrackerServlet?clickData=JnB1YklkPTIwOTc3JnNpdGVJZD0zMDE1MSZhZElkPTI2NjA0JmthZHNpemVpZD05JnRsZElkPTAmcGFzc2JhY2s9MCZjYW1wYWlnbklkPTEyMTYmY3JlYXRpdmVJZD0wJmFkU2VydmVySWQ9MjQz_url=" + url_enc);
var redirect_timeout = 300;
setTimeout('redirect("http://weeklyad.target.com")', redirect_timeout);
// -->
</script></head><body></body></html>
So the redirect happens because of redirect
function (javascript) being called and not a Location
(header) redirect.
BTW: you can see where you will be reaching by looking at the original URL, notice the &redirect=http://weeklyad.target.com
parameter
Upvotes: 1