mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
4aa6f799b2
commit
eebb94c636
@ -81,6 +81,18 @@ public class HttpDownloader {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头,多个请求头则多次调用
|
||||
*
|
||||
* @param name 请求头名
|
||||
* @param value 请求头值
|
||||
* @return this
|
||||
*/
|
||||
public HttpDownloader header(final String name, final String value) {
|
||||
this.request.header(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置
|
||||
*
|
||||
|
@ -46,7 +46,9 @@ import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 请求消息体,包括请求的URI、请求头、请求体等
|
||||
* 请求消息<br>
|
||||
* 请求消息用于定义请求所需的信息,如请求URL、请求方法、请求头、请求体等<br>
|
||||
* 此对象为无状态对象,与具体引擎不相关,因此可以复用。
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
@ -139,11 +141,6 @@ public class Request implements HeaderOperation<Request> {
|
||||
* 是否是REST请求模式,REST模式运行GET请求附带body
|
||||
*/
|
||||
private boolean isRest;
|
||||
/**
|
||||
* 重定向计数器,内部使用<br>
|
||||
* 单次请求时,重定向次数内部自增
|
||||
*/
|
||||
private int redirectCount;
|
||||
|
||||
/**
|
||||
* 默认构造
|
||||
@ -208,7 +205,8 @@ public class Request implements HeaderOperation<Request> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置重定向后的URL,用于处理相对路径
|
||||
* 更新设置重定向后的URL,用于处理相对路径<br>
|
||||
* 注意此方法会修改对象本身
|
||||
*
|
||||
* @param location 重定向后的URL
|
||||
* @return this
|
||||
|
@ -46,7 +46,7 @@ public class RequestContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求
|
||||
* 设置请求,在重新请求或重定向时,更新请求信息
|
||||
*
|
||||
* @param request 请求
|
||||
* @return this
|
||||
@ -57,16 +57,18 @@ public class RequestContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否允许重定向,如果允许,则重定向计数器+1
|
||||
* 获取重定向计数器
|
||||
*
|
||||
* @return 是否允许重定向
|
||||
* @return 重定向计数器
|
||||
*/
|
||||
public boolean canRedirect(){
|
||||
final int maxRedirects = request.maxRedirects();
|
||||
if(maxRedirects > 0 && redirectCount < maxRedirects){
|
||||
redirectCount++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
public int getRedirectCount() {
|
||||
return redirectCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向计数器+1
|
||||
*/
|
||||
public void incrementRedirectCount() {
|
||||
this.redirectCount++;
|
||||
}
|
||||
}
|
||||
|
@ -177,8 +177,9 @@ public class JdkClientEngine extends AbstractClientEngine {
|
||||
*/
|
||||
private JdkHttpResponse sendRedirectIfPossible(final JdkHttpConnection conn, final RequestContext context) {
|
||||
final Request message = context.getRequest();
|
||||
final int maxRedirects = message.maxRedirects();
|
||||
// 手动实现重定向
|
||||
if (message.maxRedirects() > 0) {
|
||||
if (maxRedirects > 0 && context.getRedirectCount() < maxRedirects) {
|
||||
final int code;
|
||||
try {
|
||||
code = conn.getCode();
|
||||
@ -189,8 +190,6 @@ public class JdkClientEngine extends AbstractClientEngine {
|
||||
}
|
||||
|
||||
if (HttpStatus.isRedirected(code)) {
|
||||
message.locationTo(conn.header(HeaderName.LOCATION));
|
||||
|
||||
// https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7
|
||||
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections
|
||||
// 307方法和消息主体都不发生变化。
|
||||
@ -198,10 +197,10 @@ public class JdkClientEngine extends AbstractClientEngine {
|
||||
// 重定向默认使用GET
|
||||
message.method(Method.GET);
|
||||
}
|
||||
|
||||
if (context.canRedirect()) {
|
||||
return doSend(context);
|
||||
}
|
||||
message.locationTo(conn.header(HeaderName.LOCATION));
|
||||
// 自增计数器
|
||||
context.incrementRedirectCount();
|
||||
return doSend(context);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,8 @@ public class OkHttpEngine extends AbstractClientEngine {
|
||||
}
|
||||
|
||||
// 自定义重定向
|
||||
if(message.maxRedirects() > 0){
|
||||
final int maxRedirects = message.maxRedirects();
|
||||
if(maxRedirects > 0 && context.getRedirectCount() < maxRedirects){
|
||||
final int code = response.code();
|
||||
if (HttpStatus.isRedirected(code)) {
|
||||
message.locationTo(response.header(HeaderName.LOCATION.getValue()));
|
||||
@ -169,10 +170,9 @@ public class OkHttpEngine extends AbstractClientEngine {
|
||||
// 重定向默认使用GET
|
||||
message.method(Method.GET);
|
||||
}
|
||||
|
||||
if (context.canRedirect()) {
|
||||
return doSend(context);
|
||||
}
|
||||
// 自增计数器
|
||||
context.incrementRedirectCount();
|
||||
return doSend(context);
|
||||
}
|
||||
|
||||
return new OkHttpResponse(response, message);
|
||||
|
@ -19,6 +19,7 @@ package org.dromara.hutool.http.client;
|
||||
import org.dromara.hutool.core.io.StreamProgress;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.http.HttpGlobalConfig;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@ -50,6 +51,13 @@ public class DownloadTest {
|
||||
Console.log("Download size: " + size);
|
||||
}
|
||||
|
||||
@Test
|
||||
void downloadWithHeaderTest() {
|
||||
HttpDownloader.of("https://hutool.cn/")
|
||||
.header(MapUtil.of("Authorization", "token"))
|
||||
.downloadFile(FileUtil.file("d:/test/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void downloadTest() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user