Where can I find an example on how to set up https in java where https is not supported

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

If someone can point me in the right direction, I would be very grateful

+2


source to share


5 answers


As bitover said, you can use the apache http components. A simple example where the https page requires 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 .

0


source


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

com.sun.midp.io.j2me.https

      

implements the HttpsConnection interface.

EDIT:



I think your best bet is to find an older version of BouncyCastle JCE that works with your Java version. BC JCE comes with this class for handling 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

+1


source


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

Br, Gabi

0


source


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

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

Good luck.

0


source


JBoss netty has a sample HTTP client , you just need to add SSlHandler to HTTPRequestHandler and HTTPResponseHandler.

0


source







All Articles