HttpClientUtil

public class HttpClientUtil {

    private static final Logger LOG = LoggerFactory.getLogger(HttpClientUtil.class);

    private final static HttpClientUtil httpClientUtil = new HttpClientUtil();

    private HttpClientUtil(){}

    public static HttpClientUtil getInstance() {return httpClientUtil;}

    public String httpGet(String url) throws IOException {

        CloseableHttpClient httpClient = createDefault();

        CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url));

        return getStringResponse(httpResponse);

    }

    public String httpGet(String url, Map<String, String> param) throws URISyntaxException, IOException {

        CloseableHttpClient httpClient = createDefault();

        URIBuilder urlBuilder = new URIBuilder(url);

        if (param != null && !param.isEmpty()) {

            for(Map.Entry<String, String> entry : param.entrySet()) {

                urlBuilder.setParameter(entry.getKey(), entry.getValue());

            }

        }

        CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(urlBuilder.build()));

        return getStringResponse(httpResponse);

    }

    public <T> T httpGet(String url, Class<T> returnClass) throws IOException {

        return getTResponse(httpGet(url), returnClass);

    }

    public <T> T httpGet(String url, Map<String, String> param, Class<T> returnClass) throws IOException, URISyntaxException {

        return getTResponse(httpGet(url, param), returnClass);

    }

    public <T> String httpPost(String url, T param, Class<T> paramClass) throws IOException {

        CloseableHttpClient httpClient = createDefault();

        HttpPost httpPost = new HttpPost(url);

        if (param != null) {

            ObjectMapper mapper = new ObjectMapper();

            try {

                StringEntity s = new StringEntity(mapper.writeValueAsString(param));

                s.setContentEncoding("UTF-8");

                s.setContentType("application/json");//发送json数据需要设置contentType

                httpPost.setEntity(s);

            } catch (JsonProcessingException e) {

                LOG.error("ObjectMapper writeValueAsString error.", e);

            }

        }

        return getStringResponse(httpClient.execute(httpPost));

    }

    public <T, V> V httpPost(String url, T param,Class<T> paramClass, Class<V> returnClass) throws IOException {

        return getTResponse(httpPost(url, param, paramClass), returnClass);

    }

    protected CloseableHttpClient createDefault() {return HttpClientBuilder.create().build();}

    protected String getStringResponse(CloseableHttpResponse httpResponse) throws IOException {

        HttpEntity httpEntity = httpResponse.getEntity();

        return httpEntity == null ? null : EntityUtils.toString(httpEntity);

    }

    private <T> T getTResponse(String httpResponse, Class<T> returnClass) throws IOException {

        if (httpResponse == null || httpResponse.length() == 0) return null;

        ObjectMapper mapper = new ObjectMapper();

        return mapper.readValue(httpResponse, returnClass);

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容