mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix #IB2U8V
This commit is contained in:
parent
cbaf8103e9
commit
0ec182e03e
@ -17,6 +17,7 @@
|
|||||||
package org.dromara.hutool.http.client.engine.okhttp;
|
package org.dromara.hutool.http.client.engine.okhttp;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.internal.http.HttpMethod;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
@ -156,7 +157,8 @@ public class OkHttpEngine extends AbstractClientEngine {
|
|||||||
// 填充方法
|
// 填充方法
|
||||||
final String method = message.method().name();
|
final String method = message.method().name();
|
||||||
final HttpBody body = message.handledBody();
|
final HttpBody body = message.handledBody();
|
||||||
if (null != body) {
|
if (null != body || HttpMethod.requiresRequestBody(method)) {
|
||||||
|
// okhttp中,POST等请求必须提供body,否则会抛异常,此处传空的OkHttpRequestBody
|
||||||
// 为了兼容支持rest请求,在此不区分是否为GET等方法,一律按照body是否有值填充,兼容
|
// 为了兼容支持rest请求,在此不区分是否为GET等方法,一律按照body是否有值填充,兼容
|
||||||
builder.method(method, new OkHttpRequestBody(body));
|
builder.method(method, new OkHttpRequestBody(body));
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,11 +47,13 @@ public class OkHttpRequestBody extends okhttp3.RequestBody {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long contentLength() throws IOException {
|
public long contentLength() throws IOException {
|
||||||
return this.body.contentLength();
|
return null == this.body ? 0 : this.body.contentLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTo(final BufferedSink bufferedSink) {
|
public void writeTo(final BufferedSink bufferedSink) {
|
||||||
body.writeClose(bufferedSink.outputStream());
|
if(null != this.body){
|
||||||
|
this.body.writeClose(bufferedSink.outputStream());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user