GET
public int verifyWechatUser(WechatUserVo wechatUser) {
String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
requestUrl = requestUrl.replace("ACCESS_TOKEN", wechatUser.getAccessToken());
requestUrl = requestUrl.replace("OPENID", wechatUser.getWechatOpenId());
HttpClient httpSSLClient = getHttpSSLClient();
HttpHost proxy = new HttpHost(this.proxyHost, this.proxyPort);
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(3000)
.build();
HttpGet httpGet = new HttpGet(requestUrl);
httpGet.setConfig(requestConfig);
HttpResponse response;
try {
response = httpSSLClient.execute(httpGet);
Map<String, Object> result = JSON.parseObject(parseString(response), new TypeReference<Map<String, Object>>() {
});
List<String> collect = result.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toList());
if (collect.contains("errcode")) {
return HttpStatusCode.NOT_ACCEPTABLE;
}
if (result.get("unionid").equals(wechatUser.getWechatUnionId())) {
KinUser kinUser = KinUser.builder().withWechatUnionId(wechatUser.getWechatUnionId()).withWechatOpenId(wechatUser.getWechatOpenId())
.withDeletedFlag(SysCasConstant.NOT_DELETED_FLAG).build();
KinUser byWechat = casKinUserService.selectOneKinUser(kinUser);
if (Optional.ofNullable(byWechat).isPresent()) {
return HttpStatusCode.CONTINUE;
} else {
return HttpStatusCode.NOT_FOUND;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return HttpStatusCode.NOT_EXTENDED;
}
POST
private void toThirdParty(InputBean inputBean) {
String requestUrl = "http://127.0.0.1:8080/thirdPartyApi";
HttpClient httpSSLClient = getHttpSSLClient();
String host = PropertyUtil.getProperty("http.client.proxy.host");
Integer port = Integer.parseInt(PropertyUtil.getProperty("http.client.proxy.port"));
HttpHost proxy = new HttpHost(host, port);
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(3000)
.build();
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.setConfig(requestConfig);
String paramBody = JSON.toJSONString(inputBean);
StringEntity postingString = null;// json传递
try {
postingString = new StringEntity(paramBody);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(postingString);
HttpResponse response;
try {
response = httpSSLClient.execute(httpPost);
Map<String, String> result = JSON.parseObject(parseString(response), new TypeReference<Map<String, String>>() {
});
LOGGER.info("返回序列化为map得到的结果:{}",result);
} catch (IOException e) {
e.printStackTrace();
}
}
getHttpSSLClient()
private static HttpClient getHttpSSLClient() {
try {
X509TrustManager trustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
ctx.init(null, new TrustManager[] { trustManager }, null);
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).setExpectContinueEnabled(Boolean.TRUE)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient closeableHttpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
.build();
return closeableHttpClient;
} catch (KeyManagementException ex) {
throw new RuntimeException(ex);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}