/* * Copyright (C), 2018-2021 * Project: starter * FileName: SSLClient * Author: Louis * Date: 2021/12/8 20:09 */ package com.fr.plugin.icak.utils; import com.fr.log.FineLoggerFactory; import com.fr.stable.StringUtils; import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.HttpHost; import com.fr.third.org.apache.http.HttpResponse; import com.fr.third.org.apache.http.client.HttpClient; import com.fr.third.org.apache.http.client.methods.HttpGet; import com.fr.third.org.apache.http.conn.ClientConnectionManager; import com.fr.third.org.apache.http.conn.scheme.Scheme; import com.fr.third.org.apache.http.conn.scheme.SchemeRegistry; import com.fr.third.org.apache.http.conn.ssl.SSLSocketFactory; import com.fr.third.org.apache.http.impl.client.DefaultHttpClient; import com.fr.third.org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.net.URI; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; /** *
* * * @author Louis * @since 1.0.0 */ public class SSLClient extends DefaultHttpClient { public SSLClient() throws Exception { super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } public static String doGet(String url, String proxyUrl) { HttpClient httpClient = null; HttpGet httpGet = null; HttpResponse response = null; String result = null; try { httpClient = new SSLClient(); httpGet = new HttpGet(url); httpGet.addHeader("Content-Type", "application/json"); if (StringUtils.isNotBlank(proxyUrl)) { URI proxyUri = new URI(proxyUrl); HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); response = httpClient.execute(proxy, httpGet); } else { response = httpClient.execute(httpGet); } if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); } } } catch (Exception e) { FineLoggerFactory.getLogger().error("GetWeChatUserInfo doGet", e); } return result; } }