fix #IB2U8V

This commit is contained in:
Looly 2024-11-12 17:17:58 +08:00
parent cbaf8103e9
commit 0ec182e03e
2 changed files with 7 additions and 3 deletions

View File

@ -17,6 +17,7 @@
package org.dromara.hutool.http.client.engine.okhttp;
import okhttp3.OkHttpClient;
import okhttp3.internal.http.HttpMethod;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.util.ObjUtil;
@ -156,7 +157,8 @@ public class OkHttpEngine extends AbstractClientEngine {
// 填充方法
final String method = message.method().name();
final HttpBody body = message.handledBody();
if (null != body) {
if (null != body || HttpMethod.requiresRequestBody(method)) {
// okhttp中POST等请求必须提供body否则会抛异常此处传空的OkHttpRequestBody
// 为了兼容支持rest请求在此不区分是否为GET等方法一律按照body是否有值填充兼容
builder.method(method, new OkHttpRequestBody(body));
} else {

View File

@ -47,11 +47,13 @@ public class OkHttpRequestBody extends okhttp3.RequestBody {
@Override
public long contentLength() throws IOException {
return this.body.contentLength();
return null == this.body ? 0 : this.body.contentLength();
}
@Override
public void writeTo(final BufferedSink bufferedSink) {
body.writeClose(bufferedSink.outputStream());
if(null != this.body){
this.body.writeClose(bufferedSink.outputStream());
}
}
}