This commit is contained in:
Looly 2022-10-26 13:12:39 +08:00
parent d2e66a1ca2
commit c2262bc40e
2 changed files with 36 additions and 1 deletions

View File

@ -85,7 +85,7 @@ public class CharSequenceResource implements Resource, Serializable {
@Override @Override
public byte[] readBytes() throws IORuntimeException { public byte[] readBytes() throws IORuntimeException {
return this.data.toString().getBytes(this.charset); return StrUtil.bytes(this.data, this.charset);
} }
} }

View File

@ -0,0 +1,35 @@
package cn.hutool.http.client.body;
import cn.hutool.core.io.resource.StringResource;
import cn.hutool.http.HttpUtil;
import java.nio.charset.Charset;
/**
* String类型的body一般用于Rest请求的请求体如JSON或XML
*
* @author looly
*/
public class StringBody extends ResourceBody {
/**
* 构造根据body内容类型自动识别Content-Type
*
* @param body Body内容
* @param charset 自定义编码
*/
public StringBody(final String body, final Charset charset) {
this(body, HttpUtil.getContentTypeByRequestBody(body), charset);
}
/**
* 构造
*
* @param body Body内容
* @param contentType 自定义Content-Type
* @param charset 自定义编码
*/
public StringBody(final String body, final String contentType, final Charset charset) {
super(new StringResource(body, contentType, charset));
}
}