【问题标题】:android https - self signed server certificate - exceptionandroid https - 自签名服务器证书 - 异常
【发布时间】:2014-08-08 21:05:28
【问题描述】:

参考链接:

http://developer.android.com/training/articles/security-ssl.html#UnknownCa http://randomizedsort.blogspot.com/2010/09/step-to-step-guide-to-programming.html

我有以下代码用于使用自签名证书连接到服务器(我使用此 answer 作为密钥库部分):

 try
 {
   final KeyStore ks = KeyStore.getInstance("BKS");
   final InputStream inputStream = getApplicationContext().getResources().openRawResource(R.raw.certs);
   ks.load(inputStream, getApplicationContext().getString(R.string.store_pass).toCharArray());
   inputStream.close();

   TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   tmf.init(ks);

   SSLContext context = SSLContext.getInstance("TLS");
   context.init(null, tmf.getTrustManagers(), new SecureRandom());

   URL url = new URL("https://www.mywebsite.com/");
   HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
   urlConnection.setSSLSocketFactory(context.getSocketFactory());
   InputStream in = urlConnection.getInputStream();
 }
 catch(Exception e) {
   Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
   // the toast appear empty (described in question)
 }

如果我使用我的网站名称:

 http://www.mywebsite.com 

然后在我周围的 try catch 中,抛出异常:

 com.android.okhttp.internal.http.HttpURLConnectionImpl 
 cannot be cast to javax.net.ssl.HttpsURLConnection

如果我使用:

 https://www.mywebsite.com

然后我仍然得到一个异常,但由于某种原因,catch(Exception e) 中的异常对象“e”有空消息(它不是 null),所以我无法找出异常。

如果我尝试使用:

 Log.d("TEST", e.getMessage());

然后我得到:

 java.lang.NullPointerException: println needs a message

有人能指出我做错了什么吗?

编辑:

这是 e.printStackTrace() 的输出

D/TEST﹕ e.printStackTrace() :
06-18 14:20:17.493      W/System.err﹕ android.os.NetworkOnMainThreadException
06-18 14:20:17.493      W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
06-18 14:20:17.493      W/System.err﹕ at com.android.authmobile.app.TestActivity$1.onClick(TestActivity.java:90)
06-18 14:20:17.493      W/System.err﹕ at android.view.View.performClick(View.java:4438)
06-18 14:20:17.497      W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
06-18 14:20:17.497      W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-18 14:20:17.497      W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-18 14:20:17.497      W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-18 14:20:17.497      W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
06-18 14:20:17.497      W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-18 14:20:17.497      W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-18 14:20:17.497      W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-18 14:20:17.497      W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-18 14:20:17.497      W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 上面显示的代码中没有catch(Exception e)。
  • @CommonsWare :添加,对此感到抱歉。
  • LogCat 中是否还有其他内容,例如由其他内容记录的堆栈跟踪?
  • @CommonsWare 编辑了异常部分(也添加了日志部分)。
  • 改用 e.printStackTrace()。

标签: android ssl


【解决方案1】:

您将收到 NetworkOnMainThreadException。较新版本的 Android 不允许您在与 UI 相同的线程中执行网络任务,因为它们可能导致您的应用程序锁定并无响应。将您的网络操作移至 AsyncTask。请参阅Connecting to the Network 的 Android 培训文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2018-12-04
    • 2013-11-09
    • 2021-02-26
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多