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 param) throws URISyntaxException, IOException {

        CloseableHttpClient httpClient = createDefault();

        URIBuilder urlBuilder = new URIBuilder(url);

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

            for(Map.Entry entry : param.entrySet()) {

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

            }

        }

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

        return getStringResponse(httpResponse);

    }

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

        return getTResponse(httpGet(url), returnClass);

    }

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

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

    }

    public String httpPost(String url, T param, Class 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 V httpPost(String url, T param,Class paramClass, Class 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 getTResponse(String httpResponse, Class returnClass) throws IOException {

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

        ObjectMapper mapper = new ObjectMapper();

        return mapper.readValue(httpResponse, returnClass);

    }

你可能感兴趣的:(HttpClientUtil)