微信小程序群发功能代码实现详细分析
784
2022-10-07
[Http]跳过SSL认证
package com.pccw.hktdcb.apiserver.util;import it.sauronsoftware.base64.Base64;import java.io.IOException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLException;import javax.net.ssl.SSLSession;import javax.net.ssl.SSLSocket;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.apache.org.slf4j.Logger;import org.slf4j.LoggerFactory;public class HttpsRequestUtil { protected static final Logger logger = LoggerFactory .getLogger(HttpsRequestUtil.class); private static final int TIME_OUT = Integer.parseInt(SystemConfig .getProperty("* 1000; public static HttpClient getHttpClient() { @SuppressWarnings("deprecation") HttpClient = new DefaultHttpClient(); try { // Secure Protocol implementation. SSLContext ctx = SSLContext.getInstance("SSL"); // Implementation of a trust manager for X509 certificates X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { // TODO Auto-generated method stub return true; } public void verify(String arg0, SSLSocket arg1) throws IOException { // TODO Auto-generated method stub } public void verify(String arg0, X509Certificate arg1) throws SSLException { // TODO Auto-generated method stub } public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException { // TODO Auto-generated method stub } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, hostnameVerifier); ClientConnectionManager ccm = // register protocol in scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("443, ssf)); sr.register(new Scheme("80, PlainSocketFactory .getSocketFactory())); // set Time out CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT); CoreConnectionPNames.SO_TIMEOUT, TIME_OUT); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } return } public static String getHTTPSRequest(String url, String userName, String password) { logger.info("HttpsRequestUtil : getHTTPSRequest start............"); logger.debug("url : " + url); String result = null; HttpGet = new HttpGet(url); try { String authString = userName + ":" + password; String authStringEnc = new String(Base64.encode(authString .getBytes())); "Basic " + authStringEnc); "application/json"); "application/json"); HttpClient = getHttpClient(); HttpResponse response = if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { int = response.getStatusLine().getStatusCode(); if (== HttpStatus.SC_OK) { result = EntityUtils.toString(resEntity, "UTF-8"); } else { logger.error( "HttpsRequestUtil : getHTTPSRequest failed, status code is [{}]", } } } logger.debug("HttpsRequestUtil : getHTTPSRequest;result = " + result); } catch (ClientProtocolException e) { logger.error( "HttpsRequestUtil : getHTTPSRequest error occur, Exception is [{}]", e.getMessage()); } catch (IOException e) { logger.error( "HttpsRequestUtil : getHTTPSRequest error occur, Exception is [{}]", e.getMessage()); } finally { } logger.info("HttpsRequestUtil : getHTTPSRequest end........"); return result; }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。