Link to home
Start Free TrialLog in
Avatar of HarikrishnaYamini
HarikrishnaYamini

asked on

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();
           connection.setRequestMethod("POST");
//           System.out.println(connection.);
           String sessionID= connection.getHeaderField("Set-Cookie");
                 
           int k=0;
//           if(( k=sessionID.indexOf("="))>0) {
//               sessionID=sessionID.substring(k+1);
//           }
           System.out.println(sessionID);


 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());
        }
    }

Avatar of hoomanv
hoomanv
Flag of Canada image

why dont u use HttpsURLConnection ?
HttpsURLConnection extends HttpURLConnection with support for https-specific features.
Avatar of HarikrishnaYamini
HarikrishnaYamini

ASKER

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
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)
   break;
 if(headerName == "Set-Cookie") {
   System.out.println(headerName + " = " + headerValue);
 }
}
I forgot to say

   String headerValue = con.getHeaderField(i);

before

   System.out.println(headerName + " = " + headerValue);
System.out.println(connection.getHeaderFields().toString());

i am getting {} map
try connection.connect();
before getting the header fileds
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)
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial