使用HttpClient調用第三方接口

最近項目中須要調用第三方的Http接口,這裏我用到了HttpClient。json

首先咱們要搞明白第三方接口中須要咱們傳遞哪些參數、數據,搞明白參數之後咱們就可使用HttpClient調用接口了。api

1.調用Post方法。
HttpClient初始化與參數設置app

public class HttpClientUtils{

    private static final Integer DEFAULT_MAX_TOTAL = 200;
    private static final Integer DEFAULT_MAX_PER_ROUTE = 20;

    private static final String TLS_V1_PROTOCAL = "TLSv1";
    private static final String TLS_V1_1_PROTOCAL = "TLSv1.1";
    private static final String TLS_V1_2_PROTOCAL = "TLSv1.2";
    private static final String SSL_V3_PROTOCAL = "SSLv3";

    public static final String HTTP_PROTOCAL = "http";
    public static final String HTTPS_PROTOCAL = "https";

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private PoolingHttpClientConnectionManager cm;
    private Registry<ConnectionSocketFactory> registry;

    private void init() {
        try {
            SSLContextBuilder builder = SSLContexts.custom();
            builder.loadTrustMaterial(null, (chain, authType) -> true);
            SSLContext sslContext = builder.build();
            SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(
                    sslContext, new String[] {TLS_V1_PROTOCAL, TLS_V1_1_PROTOCAL, TLS_V1_2_PROTOCAL, SSL_V3_PROTOCAL}, null, NoopHostnameVerifier.INSTANCE);

            registry = RegistryBuilder
                    .<ConnectionSocketFactory> create()
                    .register(HTTP_PROTOCAL, PlainConnectionSocketFactory.INSTANCE)
                    .register(HTTPS_PROTOCAL, sslSF)
                    .build();

            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(DEFAULT_MAX_TOTAL);
            cm.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);

        } catch (Exception ex) {
            logger.error("Can't initialize connection manager! Exiting");
            System.exit(FATAL_EXIT_CODE);
        }
    }

    public HttpClientUtils() {
        init();
    }

    public HttpPost requestParam(String url, String jsonStr) throws UnsupportedEncodingException {
        if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonStr)) {
            throw new InvalidParameterException("Invalid url or requestJson");
        }

        HttpPost request = new HttpPost(url);
        StringEntity se = new StringEntity(jsonStr);
        request.setEntity(se);
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
        if (StringUtils.isNotBlank(applicationProperties.getHrgenieHttpProxyHost())) {
            HttpHost proxy = new HttpHost("127.0.0.1", "8080"); // 代理主機地址與端口
            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
            request.setConfig(config);
        }
        return request;
    }

    public CloseableHttpClient getConnection() {
        return HttpClients.custom().setConnectionManager(cm).build();
    }
}

2.對象與Json互轉ide

public class CommunicateUtil {
    public static Optional<String> objToJson(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(object);
        } catch (JsonProcessingException ex) {
            ex.printStackTrace();
        }
        return Optional.ofNullable(jsonStr);
    }

    public static <T> Optional<T> jsonToObj(String json, Class<T> clz) {
        ObjectMapper mapper = new ObjectMapper();
        T objToRet = null;
        try {
            objToRet = mapper.readValue(json, clz);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return Optional.ofNullable(objToRet);
    }
}

3.執行httpclient並解析返回數據(Response 對象按照本身的需求封裝)。oop

public class CommunicateImpl implements Communicate{

    private static Logger logger = LoggerFactory.getLogger(CommunicateImpl.class);

    @Autowired
    private HttpConnectionManager httpConnectionManager;
        
        public <V, K> V communicateCategory(K k, Class<V> respClz) {
        String targetUrl= "https://xxx.xxx.com/api/v1/xxx"; // 目標第三方接口的url
        
        Optional<String> reqJson = CommunicateUtil.objToJson(k);
        HttpPost postReq = null;
        try {
            postReq = httpConnectionManager.requestParam(targetUrl, reqJson.get());
        } catch (IOException ex) {
            logger.error("Exception thrown when getting request, exception is:{}", ex);
            throw new RuntimeException("Invalid request!");
        }

        V snqResponse = null;

        CloseableHttpClient httpClient = httpConnectionManager.getConnection();
        try (CloseableHttpResponse response = httpClient.execute(postReq)) {
            snqResponse = parseResponse(response, respClz);
        } catch (IOException ex) {
            logger.error("Exception thrown when getting the response, exception is:{}", ex);
            throw new RuntimeException("Invalid request!");
        }
        return snqResponse;
    }

        @Override
    public Response communicate(Request request) {
        return communicateCategory(request, Response.class);
    }    

        private <T> T parseResponse(CloseableHttpResponse response, Class<T> clz) throws IOException {
        logger.warn("Response to parse: ", response);
        HttpEntity entity = response.getEntity();
        if (Objects.isNull(entity)) {
            throw new RuntimeException("Invalid response! Entity is null!");
        }
        String retSrc = EntityUtils.toString(entity);
        Optional<T> optRet = CommunicateUtil.jsonToObj(retSrc, clz);
        return optRet.orElseThrow(() -> new RuntimeException("Invalid response!"));
    }
public interface Communicate{
    Response communicate(Request request);
}
相關文章
相關標籤/搜索