来自 Android 应用程序的 HTTPS

发布于 2024-12-19 20:58:47 字数 5286 浏览 2 评论 0原文

我正在尝试使用 HttpClient 在 Android 手机上建立 Https 连接。由于证书是自签名的,我不断收到“javax.net.ssl.SSLException:不受信任的服务器证书”。

请帮我。 以下是代码和日志。

public class CustomClientSSLActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

        // Set verifier     
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

       // Example send http request
       final String url = "https://192.168.1.140";
       HttpPost httpPost = new HttpPost(url);
       HttpResponse response=null;
       try {
         response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("--response "+response);

    }
}

日志 - -

W/System.err(  868): javax.net.ssl.SSLException: Not trusted server certificate
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360)
W/System.err(  868):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92)
W/System.err(  868):    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:321)
W/System.err(  868):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:129)
W/System.err(  868):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(  868):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(  868):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(  868):    at com.qteq.customclientssl.CustomClientSSLActivity.onCreate(CustomClientSSLActivity.java:46)
W/System.err(  868):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err(  868):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
W/System.err(  868):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
W/System.err(  868):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
W/System.err(  868):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
W/System.err(  868):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(  868):    at android.os.Looper.loop(Looper.java:123)
W/System.err(  868):    at android.app.ActivityThread.main(ActivityThread.java:4363)
W/System.err(  868):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  868):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(  868):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/System.err(  868):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err(  868):    at dalvik.system.NativeStart.main(Native Method)
W/System.err(  868): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPat
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168)
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355)
W/System.err(  868):    ... 23 more
W/System.err(  868): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
W/System.err(  868):    at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149)
W/System.err(  868):    at java.security.cert.CertPathValidator.validate(CertPathValidator.java:211)
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164)
W/System.err(  868):    ... 24 more
I/System.out(  868): --response null
I/ActivityManager(   52): Displayed activity com.qteq.customclientssl/.CustomClientSSLActivity: 1634 ms (total 1634 ms)

I'm trying to make Https connections on the Android phones, using HttpClient. Since the certificate is self-signed I keep getting "javax.net.ssl.SSLException: Not trusted server certificate".

Please help me.
Following is the code and the log.

public class CustomClientSSLActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

        // Set verifier     
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

       // Example send http request
       final String url = "https://192.168.1.140";
       HttpPost httpPost = new HttpPost(url);
       HttpResponse response=null;
       try {
         response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("--response "+response);

    }
}

LOG---

W/System.err(  868): javax.net.ssl.SSLException: Not trusted server certificate
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360)
W/System.err(  868):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92)
W/System.err(  868):    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:321)
W/System.err(  868):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:129)
W/System.err(  868):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(  868):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(  868):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(  868):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(  868):    at com.qteq.customclientssl.CustomClientSSLActivity.onCreate(CustomClientSSLActivity.java:46)
W/System.err(  868):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err(  868):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
W/System.err(  868):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
W/System.err(  868):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
W/System.err(  868):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
W/System.err(  868):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(  868):    at android.os.Looper.loop(Looper.java:123)
W/System.err(  868):    at android.app.ActivityThread.main(ActivityThread.java:4363)
W/System.err(  868):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  868):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(  868):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/System.err(  868):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err(  868):    at dalvik.system.NativeStart.main(Native Method)
W/System.err(  868): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPat
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168)
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355)
W/System.err(  868):    ... 23 more
W/System.err(  868): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
W/System.err(  868):    at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149)
W/System.err(  868):    at java.security.cert.CertPathValidator.validate(CertPathValidator.java:211)
W/System.err(  868):    at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164)
W/System.err(  868):    ... 24 more
I/System.out(  868): --response null
I/ActivityManager(   52): Displayed activity com.qteq.customclientssl/.CustomClientSSLActivity: 1634 ms (total 1634 ms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-12-26 20:58:47

看看这两篇文章,我认为它们会对您有所帮助:)

信任所有在 HTTPS 上使用 HttpClient 的证书

在本博客中,您也有一个示例:http://blog.crazybob.org/2010/02/android- trusting-ssl-certificates.html

希望这有帮助...

Take a look at this two posts, I think they will help you :)

Trusting all certificates using HttpClient over HTTPS

And in this blog you have an example too: http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文