httppost

   private List httpPost(List requests) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List resultList = null;
        try {
            URI uri = URI.create("http://www.baidu.com");
            HttpPost httpPost = new HttpPost(uri);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();

            StringEntity entity = new StringEntity(JsonUtil.object2Json(requests), Charsets.UTF_8);
            entity.setContentType("application/json");

            httpPost.setEntity(entity);

            CloseableHttpResponse response = httpClient.execute(httpPost);
            String res = EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
            resultList = JsonUtil.json2List(res, Result.class);
            return resultList;
        } catch (Exception e) {
            logger.error("http 请求异常", e);
            throw e;
        } finally {
            logger.info("http 请求单接口 结束 request ={},response ={} spend = {}ms", requests, resultList, stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }

    }

 

你可能感兴趣的:(后端)