Android的HTTPS

编程入门 行业动态 更新时间:2024-10-24 10:21:44
本文介绍了Android的HTTPS - 自签名的服务器证书 - 例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

简称链接:

developer.android/training/articles/安全ssl.html#UnknownCa 的randomizedsort.blogspot/2010/09/step-to-step-guide-to-programming.html

我有以下的code连接到服务器的自签名证书(我用这个回答以密钥库部分):

尝试 {   最终密钥库KS = KeyStore.getInstance(BKS);   。最终的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上下文= SSLContext.getInstance(TLS);   context.init(空,tmf.getTrustManagers(),新的SecureRandom());   网址URL =新的URL(www.mywebsite/);   HttpsURLConnection的URLConnection的=(HttpsURLConnection的)url.openConnection();   urlConnection.setSSLSocketFactory(context.getSocketFactory());   在的InputStream = urlConnection.getInputStream(); } 赶上(例外五){   Toast.makeText(getApplicationContext(),e.​​getMessage(),Toast.LENGTH_LONG).show();   //敬酒显示为空(问题描述) }

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

www.mywebsite

然后在我的周围尝试捕捉,抛出一个异常:

com.android.okhttp.internal.http.HttpURLConnectionImpl 不能转换为javax.ssl​​.HttpsURLConnection中

如果我使用:

www.mywebsite

然后我仍然得到,但由于某些原因,异常物体E在赶上(例外五)异常具有空消息(其不为空),让我想不通的除外。

如果我尝试使用:

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

然后我得到:

显示java.lang.NullPointerException:需要的println消息

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

编辑:

下面是e.printStackTrace的输出()

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

解决方案

您会得到一个NetworkOnMainThreadException。 Android版的新版本不允许你在同一个线程中执行UI网络任务,因为它们会导致您的应用程序锁定,去无响应。将您的网络操作的AsyncTask的。请参阅 Android的培训文档连接到网络。

Referred links:

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

I have the following code for connecting to server with self-signed certificate (I use this answer for the keystore part) :

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("www.mywebsite/"); 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) }

If I use my website's name as :

www.mywebsite

then in my surrounding try catch, an exception is thrown :

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

If I use :

www.mywebsite

then I still get an exception, but due to some reason, the Exception object 'e' in catch(Exception e) has empty message (its not null), so I can't figure out the exception.

If I try to use :

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

then I get:

java.lang.NullPointerException: println needs a message

Can some one point out what I am doing wrong ?

Edit:

Here's the output of 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.InetAddress.lookupHostByName(InetAddress.java:385) 06-18 14:20:17.493 W/System.err﹕ at java.InetAddress.getAllByNameImpl(InetAddress.java:236) 06-18 14:20:17.493 W/System.err﹕ at java.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)

解决方案

You're getting a NetworkOnMainThreadException. Newer versions of Android do not allow you to perform network tasks in the same thread as the UI, as they can cause your app to lock up and go non-responsive. Move your network operation to an AsyncTask. See the Android training doc for Connecting to the Network.

更多推荐

Android的HTTPS

本文发布于:2023-11-08 11:33:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!