开发者

Can't get DefaultHttpClient to work with self-signed cert

I can't can't get my self-signed cert to be accepted, I'm using the following method:

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

DefaultHttpClient dummy = new DefaultHttpClient();

SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", socketFactory, 8443));

SingleClientConnManager mgr = new SingleClientConnManager(
    dummy.getParams(), registry);
DefaultHttpClient client = new DefaultHttpClient(mgr, dummy.getParams());

// make connection with 'client' now.

My server is listening on 8443 with https, I can verify this with a browser. I'm taking the abo开发者_如何学编程ve from a few other posts on the same subject:

Not sure why it's not working for me, just keep getting a "Not trusted server certificate" exception" upon connection. Any ideas?

Accepting a certificate for HTTPs on Android

Thanks


By default java trusts the certificates in the default truststore which is cacerts.
Of course your self-signed certificate, you have configured your server to send for server authentication, will be rejected as untrusted since it is not among the trusted certificates.
To get arround this you must configure the sslcontext, used by JSSE, to use your own custom truststore which will override the java's default and will contain your self-signed certificate.
This is done by loading the keystore in the trustmanager on the initialization.
Haven't worked in Android but if you see this post appache httpsclient, you can find code to use as reference, both in the question and in Oleg's answer.
Hope this helps


I've had this on numerous development servers. There is no way around this other than to not use SSL with a self signed certificate.

It just seems to be a Java security issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜