views:

332

answers:

5

Hi,

Does anyone have a good example of how to do https over http (or a socket)? The embedded java platform doesn't support https out of the box, but I'm sure it should be possible using some 3rd party libraries or source code snippets.

If anyone can point me to the correct direction I'd be very grateful

A: 

Hi,

Apache HttpCore is what you need. See more: http://hc.apache.org/httpcomponents-core/index.html

Br, Gabi

bitover
I see a lot of things about http, and just one small subsection of ssl. Are you sure https is handled natively? thanks!
Toad
+1  A: 

What profile are you using? MIDP comes with HTTS handler. I just checked the code. This package,

com.sun.midp.io.j2me.https

implements HttpsConnection interface.

EDIT:

I think your best bet is to find an old version of BouncyCastle JCE that works with your version of Java. BC JCE comes with this class to handle SSL,

http://www.bouncycastle.org/docs/docs1.3/org/bouncycastle/crypto/tls/TlsProtocolHandler.html

This test shows you how to make a simple HTTPS request.

http://www.java2s.com/Open-Source/Java-Document/Security/Bouncy-Castle/org/bouncycastle/crypto/tls/test/BasicTlsTest.java.htm

ZZ Coder
I'm not on j2me
Toad
See my edit ..........................
ZZ Coder
i'm going to check it out! thanks!
Toad
A: 

From what i can remember, with HttpComponents you just set the socket factory to use the SSLSocketFactory and it works; however, the SSLSocketFactory was added in Java 1.4.

Honestly, I would not think the Apache HttpComponents API would even work on the limited environment you described... you would most likely need an older version, from back when it was called HttpClient (I think) and part of the Jakarta Commons.

Good luck

cjstehno
A: 

JBoss netty has an example of HTTP client, you just need to add the SSlHandler in HTTPRequestHandler and HTTPResponseHandler.

elhoim
A: 

As bitover has said, you can use apache http components. Simple example where https page need user credentials:

public static void main (String[] args){
  HttpClient httpclient = new DefaultHttpClient();
  // Note that the specified port 443 corresponds with the SSL service
  ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
new AuthScope("www.moneytrackin.com", 443),
new UsernamePasswordCredentials("user", "password"));
  // Https page to access
  HttpGet httpget = new HttpGet("https://www.moneytrackin.com/api/rest/getBalance");
  HttpResponse response;

  try {
      response = httpclient.execute(httpget);

      System.out.println("State: "+response.getStatusLine().toString());
      HttpEntity entity = response.getEntity();

      if (entity != null) {
          InputStream instream = entity.getContent();
          String result= convertStreamToString(instream);
          System.out.println("Data: "+result);
          instream.close();
      }

  } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }

More examples in this blog.