首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带SSL协议的AsyncHttpClient

带SSL协议的AsyncHttpClient
EN

Stack Overflow用户
提问于 2014-02-26 15:20:32
回答 1查看 2.1K关注 0票数 0

我试图创建我的第一个Android应用程序,我有一个问题。

我有带有https协议的web服务,为了从我使用AsyncHttpClient库的服务器获取数据,

它一直在使用http,但是一旦我将它更改为https,它就会给我一个错误:

代码语言:javascript
复制
No peer certificate

我在网上做了一些研究,我找到了一些如何修复它的建议,但我是亚伯,让它发挥作用。

这只是我尝试过的一些链接:链接1 链接2

我调用web服务的代码:

代码语言:javascript
复制
    AsyncHttpClient client = new AsyncHttpClient();

    client.get(QUERY_URL,
    new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(JSONObject jsonObject) {

                Log.d("test", jsonObject.toString());
            }

            @Override
            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

                Log.e("test", "Error: " + statusCode + " " + throwable.getMessage());
            }
        });

也许还有其他的库,您建议调用https异步?

EN

回答 1

Stack Overflow用户

发布于 2014-02-26 15:39:56

这是我用于SSL网络的代码:

代码语言:javascript
复制
private class MobHawkCheck extends AsyncTask<String, Void, JSONObject> {

        protected JSONObject doInBackground(String... params) {
            JSONObject json = null;
            try {
                // Load CAs from an InputStream
                // (could be from a resource or ByteArrayInputStream or ...)
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                // From https://www.washington.edu/itconnect/security/ca/load-der.crt
                InputStream cert = mActivity.getResources().openRawResource(R.raw.mycertificate);
                InputStream caInput = new BufferedInputStream(cert);
                Certificate ca;
                try {
                    ca = cf.generateCertificate(caInput);
                    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
                } finally {
                    caInput.close();
                }
                // Create a KeyStore containing our trusted CAs
                String keyStoreType = KeyStore.getDefaultType();
                KeyStore keyStore = KeyStore.getInstance(keyStoreType);
                keyStore.load(null, null);
                keyStore.setCertificateEntry("ca", ca);

                // Create a TrustManager that trusts the CAs in our KeyStore
                String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
                tmf.init(keyStore);

                // Create an SSLContext that uses our TrustManager
                SSLContext context = SSLContext.getInstance("TLS");
                context.init(null, tmf.getTrustManagers(), null);

                // Tell the URLConnection to use a SocketFactory from our SSLContext
                URL url = new URL("https://mysecureurl.com");
                HttpsURLConnection urlConnection =
                        (HttpsURLConnection)url.openConnection();
                urlConnection.setSSLSocketFactory(context.getSocketFactory());

                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    total.append(line);
                }
                json = new JSONObject(total.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            } catch (CertificateException e) {
                e.printStackTrace();
            }
            return json;
        }

        protected void onPostExecute(JSONObject result) {
            //TODO parse the result JSONObject
        }
    }

请注意,我使用了自己的.crt文件。您应该使用服务器证书的数据生成您的服务器证书。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22045697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档