<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.12</version>
</dependency>
/**
* Json格式post请求接口调用
* @param url 接口地址
* @param jsonStr Json格式请求参数体
* @return
*/
public static String postJson(String url, JSONObject jsonStr){
String retJson = "";
try {
retJson = HttpRequest.post(url).header("Content-Type","application/json")
.timeout(10000).body(jsonStr.toJSONString()).execute().body();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return retJson;
}
该博客展示了如何利用Hutool库进行Json格式的POST接口调用。通过设置Content-Type为application/json,设置超时时间和发送Json字符串,实现了HTTP POST请求并获取返回结果。如果请求过程中出现异常,则返回异常信息。
1879

被折叠的 条评论
为什么被折叠?



