使用Spring的RestTemplate发送http请求

  ·   Java     浏览量:
  public static final String CONTENT_TYPE_JSON = "application/json";
    
  public static final String UTF8 = "UTF-8";
    
public static JSONObject doPost(String url, Object object,String wsmc) {
        RestTemplate template = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MimeType mimetype = MimeTypeUtils.parseMimeType(CONTENT_TYPE_JSON);
        MediaType mediatype = new MediaType(mimetype.getType(),mimetype.getSubtype(),Charset.forName(UTF8));
        headers.setContentType(mediatype);
        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(getParams(object,wsmc)),headers);
        JSONObject result = template.exchange(url,HttpMethod.POST,entity,JSONObject.class ).getBody();
        return result;
    }


收起 >>
使用Spring的RestTemplate发送http请求