使用HttpClient调用第三方接口

try {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://xxxxxxxxx:xxxx/xxxx");
    httpPost.addHeader("Content-Type", "application/json");// 这里请求方式设置为json,按需要修改
    String body = "{\"xxxxx\":xxxx}";
    httpPost.setEntity(new StringEntity(body));
    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    // 处理请求结果
    Map<String, Object> resultMap = JSON.parseObject(responseContent);
    response.close();
    httpClient.close();
} catch (Exception e) {
    System.out.println(e.getMessage());
}

猜你喜欢

转载自www.cnblogs.com/lpxdbk/p/9724449.html