This commit is contained in:
Looly 2024-09-06 12:13:21 +08:00
parent 262c3b7e11
commit 844e98b6fe
4 changed files with 43 additions and 47 deletions

View File

@ -21,7 +21,6 @@ import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@ -75,14 +74,11 @@ public class HttpClient4Engine extends AbstractClientEngine {
initEngine();
final HttpUriRequest request = buildRequest(message);
final CloseableHttpResponse response;
try {
response = this.engine.execute(request);
return this.engine.execute(request, response -> new HttpClient4Response(response, message.charset()));
} catch (final IOException e) {
throw new HttpException(e);
}
return new HttpClient4Response(response, message.charset());
}
@Override
@ -223,6 +219,7 @@ public class HttpClient4Engine extends AbstractClientEngine {
/**
* 构建请求配置包括重定向
*
* @param request 请求
* @return {@link RequestConfig}
*/
@ -258,7 +255,7 @@ public class HttpClient4Engine extends AbstractClientEngine {
if (readTimeout > 0) {
requestConfigBuilder.setSocketTimeout(readTimeout);
}
if(config instanceof HttpClientConfig){
if (config instanceof HttpClientConfig) {
requestConfigBuilder.setMaxRedirects(((HttpClientConfig) config).getMaxRedirects());
}

View File

@ -16,25 +16,25 @@
package org.dromara.hutool.http.client.engine.httpclient4;
import org.dromara.hutool.core.io.IORuntimeException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Response;
import org.apache.http.Header;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* HttpClient响应包装<br>
@ -42,12 +42,12 @@ import java.util.Map;
*
* @author looly
*/
public class HttpClient4Response implements Response {
public class HttpClient4Response extends SimpleWrapper<HttpResponse> implements Response {
/**
* HttpClient的响应对象
* 响应主体
*/
private final CloseableHttpResponse rawRes;
private final HttpEntity entity;
/**
* 请求时的默认编码
*/
@ -57,23 +57,24 @@ public class HttpClient4Response implements Response {
* 构造<br>
* 通过传入一个请求时的编码当无法获取响应内容的编码时默认使用响应时的编码
*
* @param rawRes {@link CloseableHttpResponse}
* @param rawRes {@link HttpResponse}
* @param requestCharset 请求时的编码
*/
public HttpClient4Response(final CloseableHttpResponse rawRes, final Charset requestCharset) {
this.rawRes = rawRes;
public HttpClient4Response(final HttpResponse rawRes, final Charset requestCharset) {
super(rawRes);
this.entity = rawRes.getEntity();
this.requestCharset = requestCharset;
}
@Override
public int getStatus() {
return rawRes.getStatusLine().getStatusCode();
return this.raw.getStatusLine().getStatusCode();
}
@Override
public String header(final String name) {
final Header[] headers = rawRes.getHeaders(name);
final Header[] headers = this.raw.getHeaders(name);
if (ArrayUtil.isNotEmpty(headers)) {
return headers[0].getValue();
}
@ -83,7 +84,7 @@ public class HttpClient4Response implements Response {
@Override
public Map<String, List<String>> headers() {
final Header[] headers = rawRes.getAllHeaders();
final Header[] headers = this.raw.getAllHeaders();
final HashMap<String, List<String>> result = new LinkedHashMap<>(headers.length, 1);
for (final Header header : headers) {
final List<String> valueList = result.computeIfAbsent(header.getName(), k -> new ArrayList<>());
@ -94,7 +95,7 @@ public class HttpClient4Response implements Response {
@Override
public long contentLength() {
return rawRes.getEntity().getContentLength();
return this.entity.getContentLength();
}
@Override
@ -105,7 +106,7 @@ public class HttpClient4Response implements Response {
@Override
public InputStream bodyStream() {
try {
return rawRes.getEntity().getContent();
return this.entity.getContent();
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@ -114,7 +115,7 @@ public class HttpClient4Response implements Response {
@Override
public String bodyStr() throws HttpException {
try {
return EntityUtils.toString(rawRes.getEntity(), charset());
return EntityUtils.toString(this.entity, charset());
} catch (final IOException e) {
throw new IORuntimeException(e);
} catch (final ParseException e) {
@ -124,7 +125,9 @@ public class HttpClient4Response implements Response {
@Override
public void close() throws IOException {
rawRes.close();
if(this.raw instanceof Closeable){
((Closeable) this.raw).close();
}
}
@Override

View File

@ -30,7 +30,6 @@ import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicHeader;
@ -82,14 +81,11 @@ public class HttpClient5Engine extends AbstractClientEngine {
initEngine();
final ClassicHttpRequest request = buildRequest(message);
final ClassicHttpResponse response;
try {
response = this.engine.executeOpen(null, request, null);
return this.engine.execute(request, (response -> new HttpClient5Response(response, message.charset())));
} catch (final IOException e) {
throw new HttpException(e);
}
return new HttpClient5Response(response, message.charset());
}
@Override

View File

@ -16,8 +16,10 @@
package org.dromara.hutool.http.client.engine.httpclient5;
import org.apache.hc.core5.http.HttpEntity;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
@ -43,12 +45,9 @@ import java.util.Map;
*
* @author looly
*/
public class HttpClient5Response implements Response {
public class HttpClient5Response extends SimpleWrapper<ClassicHttpResponse> implements Response {
/**
* HttpClient的响应对象
*/
private final ClassicHttpResponse rawRes;
private final HttpEntity entity;
/**
* 请求时的默认编码
*/
@ -62,19 +61,20 @@ public class HttpClient5Response implements Response {
* @param requestCharset 请求时的编码
*/
public HttpClient5Response(final ClassicHttpResponse rawRes, final Charset requestCharset) {
this.rawRes = rawRes;
super(rawRes);
this.entity = rawRes.getEntity();
this.requestCharset = requestCharset;
}
@Override
public int getStatus() {
return rawRes.getCode();
return this.raw.getCode();
}
@Override
public String header(final String name) {
final Header[] headers = rawRes.getHeaders(name);
final Header[] headers = this.raw.getHeaders(name);
if (ArrayUtil.isNotEmpty(headers)) {
return headers[0].getValue();
}
@ -84,7 +84,7 @@ public class HttpClient5Response implements Response {
@Override
public Map<String, List<String>> headers() {
final Header[] headers = rawRes.getHeaders();
final Header[] headers = this.raw.getHeaders();
final HashMap<String, List<String>> result = new LinkedHashMap<>(headers.length, 1);
for (final Header header : headers) {
final List<String> valueList = result.computeIfAbsent(header.getName(), k -> new ArrayList<>());
@ -95,7 +95,7 @@ public class HttpClient5Response implements Response {
@Override
public long contentLength() {
return rawRes.getEntity().getContentLength();
return this.entity.getContentLength();
}
@Override
@ -106,7 +106,7 @@ public class HttpClient5Response implements Response {
@Override
public InputStream bodyStream() {
try {
return rawRes.getEntity().getContent();
return this.entity.getContent();
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@ -115,7 +115,7 @@ public class HttpClient5Response implements Response {
@Override
public String bodyStr() throws HttpException {
try {
return EntityUtils.toString(rawRes.getEntity(), charset());
return EntityUtils.toString(this.entity, charset());
} catch (final IOException e) {
throw new IORuntimeException(e);
} catch (final ParseException e) {
@ -125,7 +125,7 @@ public class HttpClient5Response implements Response {
@Override
public void close() throws IOException {
rawRes.close();
this.raw.close();
}
@Override