mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge pull request #2961 from wxyun2/v5-dev
feat: 新增followRedirectsCookie配置,支持开启自动重定向携带cookie
This commit is contained in:
commit
afc00aa0b0
@ -90,6 +90,11 @@ public class HttpConfig {
|
||||
*/
|
||||
boolean interceptorOnRedirect;
|
||||
|
||||
/**
|
||||
* 自动重定向时是否处理cookie
|
||||
*/
|
||||
boolean followRedirectsCookie;
|
||||
|
||||
/**
|
||||
* 设置超时,单位:毫秒<br>
|
||||
* 超时包括:
|
||||
@ -297,4 +302,14 @@ public class HttpConfig {
|
||||
this.interceptorOnRedirect = interceptorOnRedirect;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动重定向时是否处理cookie
|
||||
* @param followRedirectsCookie 自动重定向时是否处理cookie
|
||||
* @return this
|
||||
*/
|
||||
public HttpConfig setFollowRedirectsCookie(boolean followRedirectsCookie) {
|
||||
this.followRedirectsCookie = followRedirectsCookie;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -26,11 +26,7 @@ import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.CookieManager;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.Proxy;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@ -866,6 +862,16 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动重定向时是否处理cookie
|
||||
* @param followRedirectsCookie 自动重定向时是否处理cookie
|
||||
* @return this
|
||||
*/
|
||||
public HttpRequest setFollowRedirectsCookie(boolean followRedirectsCookie) {
|
||||
config.setFollowRedirectsCookie(followRedirectsCookie);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大重定向次数<br>
|
||||
* 如果次数小于1则表示不重定向,大于等于1表示打开重定向
|
||||
@ -1264,7 +1270,10 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
this.httpConnection.disconnectQuietly();
|
||||
throw new HttpException(e);
|
||||
}
|
||||
|
||||
//支持自动重定向时处理cookie
|
||||
if (config.followRedirectsCookie) {
|
||||
GlobalCookieManager.store(httpConnection);
|
||||
}
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
if (HttpStatus.isRedirected(responseCode)) {
|
||||
final UrlBuilder redirectUrl;
|
||||
|
@ -9,6 +9,8 @@ import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.CookieManager;
|
||||
import java.net.HttpCookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -169,6 +171,33 @@ public class HttpRequestTest {
|
||||
Console.log(execute.getStatus(), execute.header(Header.LOCATION));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void followRedirectsCookieTrueTest() {
|
||||
final String url = "http://localhost:8888/redirect1";
|
||||
CookieManager cookieManager = new CookieManager();
|
||||
HttpRequest.setCookieManager(cookieManager);
|
||||
HttpResponse execute = HttpRequest.get(url)
|
||||
.setMaxRedirectCount(20)
|
||||
.setFollowRedirectsCookie(true)
|
||||
.execute();
|
||||
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
|
||||
Console.log(execute.getStatus(), cookies);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void followRedirectsCookieFalseTest() {
|
||||
final String url = "http://localhost:8888/redirect1";
|
||||
CookieManager cookieManager = new CookieManager();
|
||||
HttpRequest.setCookieManager(cookieManager);
|
||||
HttpResponse execute = HttpRequest.get(url)
|
||||
.setMaxRedirectCount(20)
|
||||
.execute();
|
||||
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
|
||||
Console.log(execute.getStatus(), cookies);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void addInterceptorTest() {
|
||||
|
@ -0,0 +1,29 @@
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class RedirectServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HttpUtil.createServer(8888).addAction("/redirect1", (request, response) -> {
|
||||
response.addHeader(Header.LOCATION.getValue(),"http://localhost:8888/redirect2");
|
||||
response.addHeader(Header.SET_COOKIE.getValue(),"redirect1=1; path=/; HttpOnly");
|
||||
response.send(301);
|
||||
}).addAction("/redirect2", (request, response) -> {
|
||||
response.addHeader(Header.LOCATION.getValue(),"http://localhost:8888/redirect3");
|
||||
response.addHeader(Header.SET_COOKIE.getValue(), "redirect2=2; path=/; HttpOnly");
|
||||
response.send(301);
|
||||
}).addAction("/redirect3", (request, response) -> {
|
||||
response.addHeader(Header.LOCATION.getValue(),"http://localhost:8888/redirect4");
|
||||
response.addHeader(Header.SET_COOKIE.getValue(),"redirect3=3; path=/; HttpOnly");
|
||||
response.send(301);
|
||||
}).addAction("/redirect4", (request, response) -> {
|
||||
String cookie = request.getHeader(Header.COOKIE);
|
||||
Console.log(cookie);
|
||||
response.sendOk();
|
||||
}).start();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user