android - 通过 https 信任自签名证书

标签 android ssl

我知道网上有很多教程,但是它们超出了我的知识范围,因为我是第一次使用https。 我在这里使用了emmby的答案Trusting all certificates using HttpClient over HTTPS .但是我不知道在我连接到服务器的类里面进一步的实现是如何进行的。 这是我的 HttpsConection 类中的代码 fragment

  Log.d("url", url.toString());
        HttpsURLConnection httpsConnection;

        Log.d("HTTP get", "get() called");
        try
        {
            Log.v("HttpConnection", url.toString());
            httpsConnection = (HttpsURLConnection) url.openConnection();

            if (request != null)
            {
                OutputStreamWriter wr = new OutputStreamWriter(
                        httpsConnection.getOutputStream());
                // Log.e(TAG, "created outputstream");

                wr.write(request);
                // Log.e(TAG, "request sent");
                wr.flush();
                wr.close();
            } else
            {
                Log.e("HttpConnection", "Nothing to send to server");
            }

            // Execute
            try
            {

                InputStream in = new BufferedInputStream(httpsConnection
                        .getInputStream());
                responseString = convertStreamToString(in);
                in.close();

我的 res/raw 文件夹中有一个 *.bks 文件,但我卡在那里了。

最佳答案

这是我的自签名 https 连接代码,对我来说工作正常:

        KeyStore trustStore;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            BasicHttpParams params = new BasicHttpParams();
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
            HttpsURLConnection.setDefaultHostnameVerifier(sf.getHostnameVerifier());
            HttpGet getRequest = new HttpGet(url);
            BasicHttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 10000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            int timeoutSocket = 15000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            httpClient.setParams(httpParameters);
            HttpResponse getResponse = httpClient.execute(getRequest);
            final int statusCode = getResponse.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) 
            { 
                return null;
            }
            HttpEntity getResponseEntity = getResponse.getEntity();
            String content = EntityUtils.toString(getResponseEntity);
            InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
            return is;
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        }
    return null;

关于android - 通过 https 信任自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14959469/

相关文章:

android - 如何在按下和释放时更改 ImageButton 的图像?

svn - 使用 SYSTEM 帐户 : SSL handshake failed: SSL error code -1/1/336032856 在 cygwin 上运行 svn

authentication - 与节点的连接在身份验证期间终止 - kafka

ios - 使用 AFNetworking 2.3.1 自签名 SSL 证书

matlab - 通过 Matab 系统连接 AWS 时遇到问题

java - 启动 Android 应用程序时出错

android - 如何只保存RealmObject而不保存引用的对象

android - sonar lint 插件无法读取 lint-results.xml

java - 如何去掉Android中的Bar

laravel - 推特: "Fetching the page failed because other errors",在带有 SSL 的 Forge NGINX 服务器上