-1

I am trying to use jsoup in java to get original URL of a page that I know it's short URL. Example : short url : http://wornon.tv/20602 original url : http://www1.bloomingdales.com/shop/product/bcbgmaxazria-dress-holly-blocked-sheath?ID=985138&LinkshareID=J84DHJLQkR4-TrjplRpi_nk8..LkpiI2ZA&PartnerID=LINKSHARE&cm_mmc=LINKSHARE--n--n-_-n

Can I work my way with jsoup or should I use another tool ? thank you

2 Answers2

0

Just use HttpUrlConnection and check the location header if the response code is in the 300s.

What is the fastest way to resolve a shortened link to its target URL in Java? How to get the complete URL address most efficiently?

Community
  • 1
  • 1
markbernard
  • 1,412
  • 9
  • 18
0

try

import java.io.IOException;
import java.net.HttpURLConnection;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class Test {

public static void main(String[] args) throws IOException {
    Test test = new Test();
    String redirectUrl = test.getRedrectUrl("http://wornon.tv/20602"); // will return http://wornon.tv/out.php?z=20602
    redirectUrl = test.getRedrectUrl(redirectUrl); // will return http://api.shopstyle.com/action/apiVisitRetailer?url=http%3A%2F%2Fwww1.bloomingdales.com%2Fshop%2Fproduct%2Fbcbgmaxazria-dress-holly-blocked-sheath%3FID%3D985138&pid=uid5721-3671061-71&utm_medium=widget&utm_source=Product+Link
    System.out.println(redirectUrl);
}

private String getRedrectUrl(String url) throws IOException {
    Response response = Jsoup.connect(url).followRedirects(false).execute();
    int status = response.statusCode();
    if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) {
        return response.header("location");
    }
    return null;
}
}
Manoj
  • 814
  • 8
  • 7