• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • 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();
           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());
        }
    }

0
HarikrishnaYamini
Asked:
HarikrishnaYamini
3 Solutions
 
hoomanvCommented:
why dont u use HttpsURLConnection ?
HttpsURLConnection extends HttpURLConnection with support for https-specific features.
0
 
HarikrishnaYaminiAuthor Commented:
It has got lot of abstract methods. I am not sure how to implement them specially with the certificates
0
 
hoomanvCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hoomanvCommented:
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);
 }
}
0
 
hoomanvCommented:
I forgot to say

   String headerValue = con.getHeaderField(i);

before

   System.out.println(headerName + " = " + headerValue);
0
 
HarikrishnaYaminiAuthor Commented:
System.out.println(connection.getHeaderFields().toString());

i am getting {} map
0
 
hoomanvCommented:
try connection.connect();
before getting the header fileds
0
 
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)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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