[Java] How to get the redirected final URL

Overview

You may be redirected when you request a URL. A note on how to get the final redirected URL.

code

Use okhttp3 for the library

Class to get the final redirected URL

RedirectFinalUrlResolver.java


package example;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FinalUrlResolver {

    private OkHttpClient mClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    return chain.proceed(chain.request());
                }
            })
            .build();

    public String get(String url) throws IOException {
        final Response r = mClient.newCall(new Request.Builder().url(url).build()).execute();
        return r.request().url().toString();
    }
}

** Executing class **

Example.java


package example;

import java.io.IOException;

public class Example {

    public static void main(String[] args) throws IOException {
        FinalUrlResolver o=new FinalUrlResolver();
        String url="https://qiita.com/settings/account";
        String finalUrl=o.get(url);
        System.out.println("original url="+url);
        System.out.println("final url="+finalUrl);
    }
}

Dependency (maven)

Add the following to pom.xml

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.13.1</version>
</dependency>

Execution result

When I ran ** Example.java **, I got the final redirected URL.

original url=https://qiita.com/settings/account
final url=https://qiita.com/login?redirect_to=https%3A%2F%2Fqiita.com%2Fsettings%2Faccount

Recommended Posts

[Java] How to get the redirected final URL
[Java] How to get the URL of the transition source
[Java] How to get the current directory
[Java] How to get the authority of the folder
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
How to use Java HttpClient (Get)
How to get the class name / method name running in Java
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00
[Processing × Java] How to use the class
How to install the legacy version [Java]
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Rails] How to get the URL of the transition source and redirect
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
[Java] How to use Thread.sleep to pause the program
[Java] How to get and output standard input
[Java] How to get the current date and time and specify the display format
How to get Class from Element in Java
[Java] (for MacOS) How to set the classpath
How to use the replace () method (Java Silver)
How to get and study java SE8 Gold
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to get the absolute path of a directory running in Java
[Java] Memo on how to write the source
How to create your own annotation in Java and get the value
[Java] How to easily get the longest character string of ArrayList using stream
Get the URL of the HTTP redirect destination in Java
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
[Android Studio] [Java] How to fix the screen vertically
[Java] How to get a request by HTTP communication
As of April 2018 How to get Java 8 on Mac
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
[Swift] How to get the document ID of Firebase
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables