JAVA HTTP中POST请求带参数
通过设置url地址
HashMap为参数
ResponseEntity执行文章来源地址https://www.toymoban.com/news/detail-741937.html
//请求路径
String url = "http://" + ipAddress + "/action/SearchPersonList";
//String url = "http://127.0.0.1:4523/m1/2699806-0-default/action/SearchPersonList";
//参数
Map<String, Object> mapOpen = new HashMap<>();
mapOpen.put("PersonType", 0);
mapOpen.put("Picture", 1);
Map<String, Object> params = new HashMap<>();
params.put("operator", "SearchPersonList");
params.put("info", mapOpen);
//使用 RestTemplate 发送 HTTP 请求
RestTemplate client = new RestTemplate();
//使用 POST 方式发送请求
HttpMethod method = HttpMethod.POST;
// 配置头信息
HttpHeaders headers = new HttpHeaders();
// 以 raw json 方式提交
headers.setContentType(MediaType.APPLICATION_JSON);
// 认证
headers.setBasicAuth("admin", "mcky9999");
//将请求头部和参数合成一个请求
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);
//执行 HTTP 请求
ResponseEntity<String> responseEntity = client.exchange(url, method, httpEntity, String.class);
//TODO:以下代码待用到时做返回值判断,当前认为没什么必要做判断
//返回状态码
Integer intA = responseEntity.getStatusCodeValue();
//返回Body内容
String strA = responseEntity.getBody();
String strA = responseEntity.getBody();
JSONObject jsonObject = JSONObject.parseObject(strA);
String versionInfoStr = jsonObject.getString("info");
JSONObject infoObject = JSONObject.parseObject(versionInfoStr);
String infoList = infoObject.getString("List");
List<InFoNumber> inventoryDTOs = JSON.parseArray(infoList, InFoNumber.class);
for (int i = 0;i < inventoryDTOs.size();i++){
generateImage(inventoryDTOs.get(i).getPicinfo(),inventoryDTOs.get(i).getMjCardNo());
}
文章来源:https://www.toymoban.com/news/detail-741937.html
到了这里,关于JAVA HTTP中POST请求带参数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!