0

I would like to find the final URL for these types of links using java. They are not short links as such like bit.ly or goo.gl however it is a URL wrapped in another.

Here is the type of link I would like to unshorten.

Original URL : https://www.google.com/aclk?sa=l&ai=DChcSEwj0iN_6y6PnAhUPwN4KHfPWB_EYABABGgJ3Yg&sig=AOD64_1QciSaXgHfqYgp_pg90gmtmuswtA&ctype=5&q=&ved=0ahUKEwifgNj6y6PnAhUMmhQKHdF7AKkQ1ikIPw&adurl=

Final URL :https://www.schuh.co.uk/kids/youth-nike-air-force-1-white-trainers/2701041020/?gclid=Cj0KCQiAsbrxBRDpARIsAAnnz_M_rzRXEUXD0pW0GeXUGrqHCDSqg-p0cHwQGqKiQ2OPFC5w7iiOCYoaAjfYEALw_wcB

How do I retrieve the Schuh URL?

Your help is much appreciated thanks.

Blundell
  • 75,855
  • 30
  • 208
  • 233

1 Answers1

0

You need to execute a request on the url, allow the redirects to be followed, then get the resultant url.

Like this:

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;


public class UrlRetriever {

    private final OkHttpClient httpClient;

    public UrlRetriever(OkHttpClient httpClient) {
        this.httpClient = httpClient;
    }


    public String getRootUrl(String url) {
            try (Response response = httpClient.newCall(buildRequest(url)).execute()) {
                String sourceUrl = response.request().url().toString();

                return sourceUrl;            
            } catch (IOException e) {
                System.err.println("Url retrieval failed for " + url + " (returning self), reason: " + e.getMessage());
                return url;
            }
    }


    private Request buildRequest(String url) {
        try {
            return new Request.Builder()
                    .head()
                    // Act like a browser, because Medium (and maybe others) don't redirect correctly otherwise
                    .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36")
                    .url(url)
                    .build();
        } catch (IllegalArgumentException e) {
            System.err.println("Url retrieval failed for " + url + ". reason: " + e.getMessage());
            throw e;
        }
    }
}
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • @GeorgeParkinson If you found this helpful pls remember to upvote or mark it as answered https://stackoverflow.com/help/someone-answers – Blundell Feb 15 '20 at 23:10