• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

Accessing HTTPS url from java application

How can I access SSL sites (URLs starting with https://...) just like normal HTTP URLs.

This is what i am doing, When I print the cookie it is null. Any help is appreciated

Authenticator.setDefault(new MyAuthenticator());
           URL url= new URL("https://...");          

//         Dynamically register the JSSE provider.
           java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

//            Set this property to use Sun's reference implementation of the HTTPS protocol.
           System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");          
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//           System.out.println(connection.);
           String sessionID= connection.getHeaderField("Set-Cookie");
           int k=0;
//           if(( k=sessionID.indexOf("="))>0) {
//               sessionID=sessionID.substring(k+1);
//           }

 public static class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        protected PasswordAuthentication getPasswordAuthentication() {
            System.out.println("user authenticated");
            // Get the username from the user...
            String username = "userName";
            // Get the password from the user...
            String password = "password";
            // Return the information
            return new PasswordAuthentication(username, password.toCharArray());

3 Solutions
why dont u use HttpsURLConnection ?
HttpsURLConnection extends HttpURLConnection with support for https-specific features.
HarikrishnaYaminiAuthor Commented:
It has got lot of abstract methods. I am not sure how to implement them specially with the certificates
The openConnection() method in the URL class returns the appropriate concrete subclass to read the specified URL. This will be either a subclass of HttpURLConnection or HttpsURLConnection

url = new URL("https://.......");
connection = (HttpsURLConnection) url.openConnection(); // would create HttpsURLConnection for you
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

also connection.getHeaderField(String name);

If called on a connection that sets the same header multiple times with possibly different values, only the last value is returned.

iterate through the header fields instead

for (int i = 1; ; i++) {
 String headerName = con.getHeaderFieldKey(i);
 if(headerName == null)
 if(headerName == "Set-Cookie") {
   System.out.println(headerName + " = " + headerValue);
I forgot to say

   String headerValue = con.getHeaderField(i);


   System.out.println(headerName + " = " + headerValue);
HarikrishnaYaminiAuthor Commented:

i am getting {} map
try connection.connect();
before getting the header fileds
HarikrishnaYaminiAuthor Commented:
I get this exception

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certifica
te found
      at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_ax.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.j(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(DashoA12275)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(DashoA12275)
error [Ljava.lang.StackTraceElement;@1171b26
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA12275)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(DashoA12275)
      at com.uprr.app.ena3.endpoints.adapters.im.HttpTunnelling.main(HttpTunnelling.java:40)
Caused by: sun.security.validator.ValidatorException: No trusted certificate found
      at sun.security.validator.SimpleValidator.buildTrustedChain(SimpleValidator.java:304)
      at sun.security.validator.SimpleValidator.engineValidate(SimpleValidator.java:107)
      at sun.security.validator.Validator.validate(Validator.java:202)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(DashoA12275)
      at com.sun.net.ssl.internal.ssl.JsseX509TrustManager.checkServerTrusted(DashoA12275)
Mayank SAssociate Director - Product EngineeringCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now