2

I need to create application for my customer, and one part from app uses https connection:

public static void getVersions() throws IOException, ParserConfigurationException, SAXException {

     URL u = new URL(VERSION_URL);
     HttpsURLConnection c = (HttpsURLConnection) u.openConnection();
     c.setRequestMethod("GET");
     c.setDoOutput(true);
     c.connect();
     InputStream in = c.getInputStream();

     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = null;
     builder = factory.newDocumentBuilder();                                   
     Document d = builder.parse(in);
}

But when I try to use it I will got IO Exception: "No trusted certificate java". It's service of my customer, all is legal. How can I fix this bug?

Robert
  • 39,162
  • 17
  • 99
  • 152
user1023177
  • 641
  • 3
  • 10
  • 18

1 Answers1

0

You can put the custom server certificate in a trust store and include it as resource within your application.

When creating the HTTPS connection to that server you can specify that this trust store contains certificates that are trusted. That can be done by creating your own SSLSocketFactory instance

KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = context.getResources().openRawResource(R.raw.mystore);
trusted.load(in, "mypassword".toCharArray());
in.close();
SSLSocketFactory mySslFact = new SSLSocketFactory(trusted);

See also: How Can I Access an SSL Connection Through Android?

Community
  • 1
  • 1
Robert
  • 39,162
  • 17
  • 99
  • 152
  • Thank you. Do you know any tricks for disabling verification? – user1023177 Nov 05 '11 at 12:48
  • Yes, but they are nonsense because you loose half of the security you gain from using SSL. Use the solution as I have described it together with a custom certificate that contains the correct dns name. Then you don't need to disable verification. – Robert Nov 05 '11 at 12:50
  • Thank you again, but can you give me anything trick for solving my problem, but now security isn't important, I need to make working prototype of application. Thank you. – user1023177 Nov 05 '11 at 12:53