Android SSL https post

2019-07-02 01:19发布

问题:

I've seen a lot of similar question here but couldn't find the solution. I have a webservice PHP file hosted at x10 premium. I bought an SSL certificate from them and can access my webpage via https on a browser and on the iPhone app. I'm having trouble doing the same on the Android app.

I tried many solutions, but keep getting SSL errors. I tried this tutorial http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html but I'm getting

10-04 07:21:57.081: W/System.err(6804): javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x2a1a11a8: Failure in SSL library, usually a protocol error
10-04 07:21:57.081: W/System.err(6804): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x471f5756:0x00000000)
10-04 07:21:57.091: W/System.err(6804):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
10-04 07:21:57.123: W/System.err(6804):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init (OpenSSLSocketImpl.java:647)
10-04 07:21:57.123: W/System.err(6804):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
10-04 07:21:57.123: W/System.err(6804):     at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:70)
10-04 07:21:57.131: W/System.err(6804):     at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
10-04 07:21:57.131: W/System.err(6804):     at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
10-04 07:21:57.131: W/System.err(6804):     at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
10-04 07:21:57.131: W/System.err(6804):     at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
10-04 07:21:57.131: W/System.err(6804):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:172)
10-04 07:21:57.141: W/System.err(6804):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-04 07:21:57.141: W/System.err(6804):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-04 07:21:57.141: W/System.err(6804):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-04 07:21:57.141: W/System.err(6804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-04 07:21:57.151: W/System.err(6804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-04 07:21:57.151: W/System.err(6804):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-04 07:21:57.201: W/System.err(6804):     at com.lablabla.parkme.ParkMeActivity.performUpdate(ParkMeActivity.java:233)
10-04 07:21:57.211: W/System.err(6804):     at com.lablabla.parkme.ParkMeActivity.access$8(ParkMeActivity.java:220)
10-04 07:21:57.211: W/System.err(6804):     at com.lablabla.parkme.ParkMeActivity$3.onClick(ParkMeActivity.java:215)
10-04 07:21:57.211: W/System.err(6804):     at android.view.View.performClick(View.java:4084)
10-04 07:21:57.221: W/System.err(6804):     at android.view.View$PerformClick.run(View.java:16966)
10-04 07:21:57.221: W/System.err(6804):     at android.os.Handler.handleCallback(Handler.java:615)
10-04 07:21:57.221: W/System.err(6804):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 07:21:57.241: W/System.err(6804):     at android.os.Looper.loop(Looper.java:137)
10-04 07:21:57.241: W/System.err(6804):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 07:21:57.251: W/System.err(6804):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 07:21:57.251: W/System.err(6804):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 07:21:57.271: W/System.err(6804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 07:21:57.271: W/System.err(6804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 07:21:57.281: W/System.err(6804):     at dalvik.system.NativeStart.main(Native Method)
10-04 07:21:57.291: W/System.err(6804): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x2a1a11a8: Failure in SSL library, usually a protocol error
10-04 07:21:57.291: W/System.err(6804): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x471f5756:0x00000000)
10-04 07:21:57.311: W/System.err(6804):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
10-04 07:21:57.311: W/System.err(6804):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)

I tried other suggestions here on SO, for accepting all certificates, since I'm making a call to my own webservice and trust all certificates.

Another thing, I can access in a browser and the iPhone app to my webservice via HTTP connection. But when I try to make it in Android, I'm receiving back HTML response which contains

10-04 07:29:47.662: D/RESULT(6900):  instead of REQUEST_URI, we could show absolute URL via:
10-04 07:29:47.662: D/RESULT(6900):  http://HTTP_HOST/REQUEST_URI
10-04 07:29:47.662: D/RESULT(6900):     but what if its https:// or other protocol?
10-04 07:29:47.662: D/RESULT(6900):     
10-04 07:29:47.662: D/RESULT(6900):     SERVER_PORT_SECURE doesn't seem to be used
10-04 07:29:47.662: D/RESULT(6900):     SERVER_PORT logic would break if they use alternate ports
10-04 07:29:47.662: D/RESULT(6900): -->
10-04 07:29:47.662: D/RESULT(6900): <h1>404 Not Found</h1>

Any ideas?

Thanks!

回答1:

There is a known problem with certificates and Android. Read this thread: Not trusted certificate using ksoap2-android

There is a solution to use HTTPs on Android.

[]s Neto