This commit is contained in:
Looly 2024-12-03 13:02:20 +08:00
parent 4aa6f799b2
commit eebb94c636
6 changed files with 48 additions and 29 deletions

View File

@ -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;
}
/**
* 设置配置
*

View File

@ -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

View File

@ -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++;
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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() {