This commit is contained in:
Looly 2023-09-15 11:59:03 +08:00
parent a04775781a
commit 9a088883a1
5 changed files with 51 additions and 61 deletions

View File

@ -215,6 +215,12 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>

View File

@ -42,7 +42,6 @@ import java.util.List;
public class JdkClientEngine implements ClientEngine {
private ClientConfig config;
private JdkHttpConnection conn;
/**
* 重定向次数计数器内部使用
*/
@ -56,10 +55,6 @@ public class JdkClientEngine implements ClientEngine {
@Override
public JdkClientEngine init(final ClientConfig config) {
this.config = config;
if(null != this.conn){
this.conn.disconnectQuietly();
this.conn = null;
}
return this;
}
@ -76,16 +71,16 @@ public class JdkClientEngine implements ClientEngine {
* @return {@link Response}
*/
public JdkHttpResponse send(final Request message, final boolean isAsync) {
initConn(message);
final JdkHttpConnection conn = buildConn(message);
try {
doSend(message);
doSend(conn, message);
} catch (final IOException e) {
// 出错后关闭连接
IoUtil.closeQuietly(this);
IoUtil.closeQuietly(conn);
throw new IORuntimeException(e);
}
return sendRedirectIfPossible(message, isAsync);
return sendRedirectIfPossible(conn, message, isAsync);
}
@Override
@ -95,9 +90,7 @@ public class JdkClientEngine implements ClientEngine {
@Override
public void close() {
if (null != conn) {
conn.disconnectQuietly();
}
// do nothing
}
/**
@ -106,28 +99,16 @@ public class JdkClientEngine implements ClientEngine {
* @param message 请求消息
* @throws IOException IO异常
*/
private void doSend(final Request message) throws IOException {
private void doSend(final JdkHttpConnection conn, final Request message) throws IOException {
final HttpBody body = message.body();
if (null != body) {
// 带有消息体一律按照Rest方式发送
body.writeClose(this.conn.getOutputStream());
body.writeClose(conn.getOutputStream());
return;
}
// 非Rest简单GET请求
this.conn.connect();
}
/**
* 初始化连接对象
*
* @param message 请求消息
*/
private void initConn(final Request message) {
// 执行下次请求时自动关闭上次请求常用于转发
IoUtil.closeQuietly(this);
this.conn = buildConn(message);
conn.connect();
}
/**
@ -163,11 +144,11 @@ public class JdkClientEngine implements ClientEngine {
/**
* 调用转发如果需要转发返回转发结果否则返回{@code null}
*
* @param conn {@link JdkHttpConnection}}
* @param isAsync 最终请求是否异步
* @return {@link JdkHttpResponse}无转发返回 {@code null}
*/
private JdkHttpResponse sendRedirectIfPossible(final Request message, final boolean isAsync) {
final JdkHttpConnection conn = this.conn;
private JdkHttpResponse sendRedirectIfPossible(JdkHttpConnection conn, final Request message, final boolean isAsync) {
// 手动实现重定向
if (message.maxRedirectCount() > 0) {
final int code;
@ -175,7 +156,7 @@ public class JdkClientEngine implements ClientEngine {
code = conn.getCode();
} catch (final IOException e) {
// 错误时静默关闭连接
conn.disconnectQuietly();
conn.closeQuietly();
throw new HttpException(e);
}
@ -191,7 +172,7 @@ public class JdkClientEngine implements ClientEngine {
}
// 最终页面
return new JdkHttpResponse(this.conn, true, message.charset(), isAsync,
return new JdkHttpResponse(conn, true, message.charset(), isAsync,
isIgnoreResponseBody(message.method()));
}

View File

@ -22,6 +22,7 @@ import org.dromara.hutool.http.meta.Method;
import org.dromara.hutool.http.ssl.SSLInfo;
import javax.net.ssl.HttpsURLConnection;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -37,7 +38,7 @@ import java.util.Map;
*
* @author Looly
*/
public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection> {
public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection>, Closeable {
private final URL url;
private final Proxy proxy;
@ -315,34 +316,6 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection> {
return this;
}
/**
* 静默断开连接不抛出异常
*
* @return this
* @since 4.6.0
*/
public JdkHttpConnection disconnectQuietly() {
try {
disconnect();
} catch (final Throwable e) {
// ignore
}
return this;
}
/**
* 断开连接
*
* @return this
*/
public JdkHttpConnection disconnect() {
if (null != this.conn) {
this.conn.disconnect();
}
return this;
}
/**
* 获得输入流对象<br>
* 输入流对象用于读取数据
@ -419,4 +392,31 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection> {
return sb.toString();
}
/**
* 静默断开连接不抛出异常
*
* @return this
* @since 4.6.0
*/
public JdkHttpConnection closeQuietly() {
try {
close();
} catch (final Throwable e) {
// ignore
}
return this;
}
/**
* 断开连接
*
*/
@Override
public void close() {
if (null != this.conn) {
this.conn.disconnect();
}
}
}

View File

@ -217,7 +217,7 @@ public class JdkHttpResponse implements Response, Closeable {
// 关闭流
IoUtil.closeQuietly(this.body);
// 关闭连接
this.httpConnection.disconnectQuietly();
this.httpConnection.closeQuietly();
}
@Override

View File

@ -21,6 +21,9 @@ import java.io.File;
* @since 4.5.16
*/
public class WordUtil {
/**
* 创建Word 07格式的生成器
*