• Java.HttpClient绕过Https证书解决方案一


    方案1

    import javax.net.ssl.*;
    import java.io.*;
    import java.net.URL;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    /**
     * Https助手
     */
    public class HttpsUtil {
    
        private static final class DefaultTrustManager implements 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;
            }
        }
    
        private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException {
            SSLContext ctx = null;
            try {
                ctx = SSLContext.getInstance("TLS");
                ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            SSLSocketFactory ssf = ctx.getSocketFactory();
    
            URL url = new URL(uri);
            HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
            //绕过HTTPS相关证书关键代码-开始
            httpsConn.setSSLSocketFactory(ssf);
            //绕过HTTPS相关证书关键代码-结束
            httpsConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            httpsConn.setRequestMethod(method);
            httpsConn.setDoInput(true);
            httpsConn.setDoOutput(true);
            return httpsConn;
        }
    
        private static byte[] getBytesFromStream(InputStream is) throws IOException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] kb = new byte[1024];
            int len;
            while ((len = is.read(kb)) != -1) {
                baos.write(kb, 0, len);
            }
            byte[] bytes = baos.toByteArray();
            baos.close();
            is.close();
            return bytes;
        }
    
        private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            byte[] kb = new byte[1024];
            int len;
            while ((len = bais.read(kb)) != -1) {
                os.write(kb, 0, len);
            }
            os.flush();
            os.close();
            bais.close();
        }
    
        public static byte[] doGet(String uri) throws IOException {
            HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
            return getBytesFromStream(httpsConn.getInputStream());
        }
    
        public static byte[] doPost(String uri, String data) throws IOException {
            HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
            setBytesToStream(httpsConn.getOutputStream(), data.getBytes());
            return getBytesFromStream(httpsConn.getInputStream());
        }
    }

    测试代码

             String address = "https网页地址";
    
            //绕过Https证书方案1
            byte[] resultBytes=HttpsUtil.doGet(address);
            String result=new String(resultBytes);
            System.out.println(result);    
            //只要请求过程中没发生异常,就说明成功绕过Https证书问题;在上例子中红色文字部分是关键
  • 相关阅读:
    案例8高级打砖块游戏
    案例17——Flash地图应用
    案例11——用遮障制作聚光灯效果及冷却动画
    案例1:用Flash CS工具制作进度条
    Flex 中可以应用于 ActionScript 类的元标签
    案例7用Flash Builder做虚拟计算器
    案例16——没特效还玩毛个Flash
    案例9不要在加载时面对苍白的屏幕
    案例15——游戏物品栏的制作(AS3高级应用)
    案例2心跳模拟
  • 原文地址:https://www.cnblogs.com/oumi/p/9161683.html
Copyright © 2020-2023  润新知