java发送POST请求

1、无header请求

public String SendJob(String url, JSONObject spotJsonObject)  {

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> ret = restTemplate.postForEntity(url, spotJsonObject, String.class);
    HttpStatus statusCode = ret.getStatusCode();
    String body = ret.getBody();
    System.out.println("返回的状态码是------> " + statusCode + " ,返回的内容是--->" + body);
    return body;
    
}

2、带header请求

public JSONObject sendPostRequestWithToken(String url, JSONObject spotJsonObject, String token) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", token);
    HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(spotJsonObject, headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, entity, JSONObject.class);
    log.info("response is:{}", response);
    return response.getBody();
}
上一篇:android简单的夜间模式


下一篇:restTemplate源码解析(四)执行ClientHttpRequest请求对象