Apache HttpClient 4.5 Redirect Handling Requests Example
HttpClient handles all types of redirects automatically, except those explicitly prohibited by the HTTP specification as requiring user intervention. See Other (status code 303) redirects on POST and PUT requests are converted to GET requests as required by the HTTP specification. One can use a custom redirect strategy to relaxe restrictions on automatic redirection of POST methods imposed by the HTTP specification. In the following tutorial we’ll us the LaxRedirectStrategy to handle http redirects.
Maven dependencies
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.apache.httpclient</groupId> <artifactId>redirect-handling</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>httpclient - ${project.artifactId}</name> <dependencies> <!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
HttpClient Handling Redirects
In the following example we make an HTTP GET to the resource http://httpbin.org/redirect/3. This resources redirects to another resource x times. We can obtain the final Http location using URIUtils.resolve(httpGet.getURI(), target, redirectLocations).
package com.memorynotfound.httpclient; import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.URIUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.List; /** * This example demonstrates the use of {@link HttpGet} request method. * and handling redirect strategy with {@link LaxRedirectStrategy} */ public class HttpClientRedirectHandlingExample { public static void main(String... args) throws IOException, URISyntaxException { CloseableHttpClient httpclient = HttpClients.custom() .setRedirectStrategy(new LaxRedirectStrategy()) .build(); try { HttpClientContext context = HttpClientContext.create(); HttpGet httpGet = new HttpGet("http://httpbin.org/redirect/3"); System.out.println("Executing request " + httpGet.getRequestLine()); System.out.println("----------------------------------------"); httpclient.execute(httpGet, context); HttpHost target = context.getTargetHost(); List<URI> redirectLocations = context.getRedirectLocations(); URI location = URIUtils.resolve(httpGet.getURI(), target, redirectLocations); System.out.println("Final HTTP location: " + location.toASCIIString()); } finally { httpclient.close(); } } }
Output
The previous application prints the following info to the console.
Executing request GET http://httpbin.org/redirect/3 HTTP/1.1 ---------------------------------------- Final HTTP location: http://httpbin.org/get
Download
From:一号门
COMMENTS