2

I'm trying to connect to my server over SSL port 443 without a certificate.

I'm getting an error thrown:

javax.net.ssl.SSLException: Not trusted server certificate

Reading other questions to solve the problem, the following code should work, but I'm still getting the error message. What could I be doing wrong?

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);
httpclient = new DefaultHttpClient(mgr, client.getParams());

// Set verifier     
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpGet httpget = new HttpGet(this.requestedURL);
httpget.addHeader(new BasicScheme().authenticate(creds, httpget));

try
{
    response = httpclient.execute(httpget);
}
catch(java.lang.Throwable t) {}
p.campbell
  • 98,673
  • 67
  • 256
  • 322
kireol
  • 703
  • 1
  • 9
  • 22
  • See this answer here on Stack Overflow: http://stackoverflow.com/questions/995514/https-connection-android#1000205 – Stefan Arentz Aug 27 '11 at 03:44
  • St3fan: That example does not go as far as the .execute. So, I tried it anyway, and it still fails. I copied it, pasted it, and added the execute right after the code. Still fails. – kireol Aug 27 '11 at 04:17
  • If I am not wrong, you are trying to connect to the Server URL from Java code. If so are you using a Self Signed Certificate? Or Do you have certificate generated from Third Party like versign, godaddy? I don't see anything related in your code about the root certificate, do you have any root certificate in keystore? –  Aug 27 '11 at 03:57
  • Once look at this post. It is very good answer for HTTPS over 443 [Click here](http://stackoverflow.com/questions/7105681/https-connection-with-basic-auth-result-into-unauthorized/7157404#7157404) – Dharmendra Aug 27 '11 at 04:24

2 Answers2

0

Your client truststore doesn't trust the server certificate. It is probably a self-signed certificate, so you need to import it into your clients truststore. Or get it signed by a CA. Ignoring the server certificate isn't secure, you may as well not use HTTPS at all.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

After trying all other solutions, android 2.2 + needs special code. This worked

Custom SSL handling stopped working on Android 2.2 FroYo

Community
  • 1
  • 1
kireol
  • 703
  • 1
  • 9
  • 22