HttpRequest 发送post
时间: 2023-09-27 17:08:51 浏览: 113
HttpRequest发送POST请求的代码如下:
```java
public static String sendPostRequest(String url, JSONObject body) {
String respStr = null;
try {
respStr = HttpRequest.post(url)
.header("Content-Type", "application/json")
.body(body.toJSONString())
.timeout(5000)
.execute()
.body();
} catch (HttpException e) {
return null;
}
return respStr;
}
```
这个方法使用了HttpRequest库的post方法,可以发送带有JSON格式数据的POST请求。需要传入URL和请求体作为参数,然后设置Content-Type为application/json,并把请求体转换为JSON字符串。最后通过execute方法发送请求并获取响应体。如果发生异常,则返回null。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [HttpRequest发送网络请求POST/GET](https://blog.csdn.net/weixin_43776126/article/details/128898456)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文