This commit is contained in:
Looly 2024-05-29 19:58:02 +08:00
parent 0d0727d0f9
commit a063a2aa99

View File

@ -173,6 +173,28 @@ public class HttpUtil {
.bodyStr();
}
/**
* 发送post请求<br>
* 请求体body参数支持两种类型
*
* <pre>
* 1. 标准参数例如 a=1&amp;b=2 这种格式
* 2. Rest模式此时body需要传入一个JSON或者XML字符串Hutool会自动绑定其对应的Content-Type
* </pre>
*
* @param urlString 网址
* @param body post表单数据
* @param timeout 超时时长-1表示默认超时单位毫秒
* @return 返回数据
*/
@SuppressWarnings("resource")
public static String post(final String urlString, final String body, final int timeout) {
// 创建自定义客户端
return ClientEngineFactory.createEngine()
.init(ClientConfig.of().setConnectionTimeout(timeout).setReadTimeout(timeout))
.send(Request.of(urlString).method(Method.POST).body(body)).bodyStr();
}
/**
* 使用默认HTTP引擎发送HTTP请求
*