Java.HttpClient繞過Https認證解決方案一

來源:互聯網
上載者:User

標籤:網頁   new t   成功   java.net   server   open   tac   iss   write   

方案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認證問題;在上例子中紅色文字部分是關鍵

 

Java.HttpClient繞過Https認證解決方案一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.