使用HttpClient调用第三方接口
最近项目中需要调用第三方的Http接口,这里我用到了HttpClient。
首先我们要搞明白第三方接口中需要我们传递哪些参数、数据,搞明白参数以后我们就可以使用HttpClient调用接口了。
1.调用Post方法。
HttpClient初始化与参数设置
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互转
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 对象按照自己的需求封装)。
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);
}