Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Understanding how URLs are redirected is crucial in web development and network programming tasks, such as handling HTTP redirects, validating URL redirects, or extracting final destination URLs. In Java, we can accomplish this functionality using HttpURLConnection or HttpClient libraries.

In this tutorial, we’ll explore different approaches to finding the redirected URL of a given URL in Java.

2. Using HttpURLConnection

Java provides the HttpURLConnection class, which allows us to make HTTP requests and handle responses. Also, we can make use of the HttpURLConnection to find the redirected URL of a given URL. Here’s how it can be done:

String canonicalUrl = "http://www.baeldung.com/";
String expectedRedirectedUrl = "https://www.baeldung.com/";

@Test
public void givenOriginalUrl_whenFindRedirectUrlUsingHttpURLConnection_thenCorrectRedirectedUrlReturned() throws IOException {
    URL url = new URL(canonicalUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setInstanceFollowRedirects(true);
    int status = connection.getResponseCode();
    String redirectedUrl = null;
    if (status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_MOVED_TEMP) {
        redirectedUrl = connection.getHeaderField("Location");
    }
    connection.disconnect();
    assertEquals(expectedRedirectedUrl, redirectedUrl);
}

Here, we define the original URL string (canonicalUrl) and the redirected URL (expectedRedirectedUrl). Then, we create an HttpURLConnection object and open a connection to the original URL.

Afterward, we set the instanceFollowRedirects property to true to enable automatic redirection. Upon receiving the response, we examine the status code to determine if redirection occurred. If redirection occurs, we retrieve the redirected URL from the “Location” header.

Finally, we disconnect the connection and assert that the retrieved redirected URL matches the expected one.

3. Using Apache HttpClient

Alternatively, we can use Apache HttpClient, a more versatile HTTP client library for Java. Apache HttpClient provides more advanced features and better support for handling HTTP requests and responses. Here’s how we can find the redirected URL using Apache HttpClient:

@Test
public void givenOriginalUrl_whenFindRedirectUrlUsingHttpClient_thenCorrectRedirectedUrlReturned() throws IOException {
    RequestConfig config = RequestConfig.custom()
      .setRedirectsEnabled(false)
      .build();
    try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build()) {
        HttpGet httpGet = new HttpGet(canonicalUrl);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            int statusCode = response.getStatusLine().getStatusCode();
            String redirectedUrl = null;
            if (statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_MOVED_TEMP) {
                org.apache.http.Header[] headers = response.getHeaders("Location");
                if (headers.length > 0) {
                    redirectedUrl = headers[0].getValue();
                }
            }
            assertEquals(expectedRedirectedUrl, redirectedUrl);
        }
    }
}

In this test, we begin by configuring the request to deactivate automatic redirection. Subsequently, we instantiate a CloseableHttpClient and dispatch an HttpGet request to the original URL.

Upon obtaining the response, we analyze the status code and headers to ascertain whether redirection occurred. In the event of redirection, we extract the redirected URL from the Location header.

4. Conclusion

In this article, we discussed how handling redirection correctly is essential for building robust web applications that interact with external resources. We’ve explored how to find the redirected URL of a given URL using both HttpURLConnection and Apache HttpClient.

As usual, the accompanying source code can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments