导入ca和crt文件到android studio

时间:2018-11-01 07:46:02

标签: android-studio https socket.io

我有.crt和.ca文件。我尝试粘贴到res / raw文件,并从socket.io创建一个套接字。用于https的目的。如果从空项目android studio开始,我该怎么办?

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题:

  • 代码再次跟随此视频:https://www.youtube.com/watch?v=YQgbxOY3lIo
  • 并且:

        HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv =
                        HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("example.com", session);
            }
        };
       OkHttpClient okHttpClient = new OkHttpClient.Builder()
       .hostnameVerifier(myHostnameVerifier)
       .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager)
       .build();    
       // default settings for all sockets
       IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
       IO.setDefaultOkHttpCallFactory(okHttpClient);        
       // set as an option
       opts = new IO.Options();
       opts.callFactory = okHttpClient;
       opts.webSocketFactory = okHttpClient;
       socket = IO.socket("https://example.com", opts);
    

context.init(null,tmf.getTrustManagers(),null);下面的connect()内部 并且{coin InputStream caInput = getAssets().open("yourca.crt");与未自签名的ssl。而已。希望它能使这个论坛更好,这太冷了!