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(n ew MyAuthenticator());
URL url= new URL("https://...");
// Dynamically register the JSSE provider.
java.security.Security.add Provider(n ew com.sun.net.ssl.internal.s sl.Provide r());
// Set this property to use Sun's reference implementation of the HTTPS protocol.
System.setProperty("java.p rotocol.ha ndler.pkgs ", "com.sun.net.ssl.internal. www.protocol");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMetho d("POST");
// System.out.println(connect ion.);
String sessionID= connection.getHeaderField( "Set-Cooki e");
int k=0;
// if(( k=sessionID.indexOf("="))> 0) {
// sessionID=sessionID.substr ing(k+1);
// }
System.out.println(session ID);
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(use rname, password.toCharArray());
}
}
This is what i am doing, When I print the cookie it is null. Any help is appreciated
Authenticator.setDefault(n
URL url= new URL("https://...");
// Dynamically register the JSSE provider.
java.security.Security.add
// Set this property to use Sun's reference implementation of the HTTPS protocol.
System.setProperty("java.p
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMetho
// System.out.println(connect
String sessionID= connection.getHeaderField(
int k=0;
// if(( k=sessionID.indexOf("="))>
// sessionID=sessionID.substr
// }
System.out.println(session
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(use
}
}
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
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(headerN ame + " = " + headerValue);
}
}
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(headerN
}
}
I forgot to say
String headerValue = con.getHeaderField(i);
before
System.out.println(headerN ame + " = " + headerValue);
String headerValue = con.getHeaderField(i);
before
System.out.println(headerN
ASKER
System.out.println(connect ion.getHea derFields( ).toString ());
i am getting {} map
i am getting {} map
try connection.connect();
before getting the header fileds
before getting the header fileds
ASKER
I get this exception
javax.net.ssl.SSLHandshake Exception: sun.security.validator.Val idatorExce ption: No trusted certifica
te found
at com.sun.net.ssl.internal.s sl.BaseSSL SocketImpl .a(DashoA1 2275)
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SunJSSE _az.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SunJSSE _az.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SunJSSE _ax.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.j(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.sta rtHandshak e(DashoA12 275)
at sun.net.www.protocol.https.HttpsClient.afterConnect(DashoA12275)
error [Ljava.lang.StackTraceElem ent;@1171b 26
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA12275)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(DashoA12275)
at com.uprr.app.ena3.endpoint s.adapters .im.HttpTu nnelling.m ain(HttpTu nnelling.j ava:40)
Caused by: sun.security.validator.Val idatorExce ption: No trusted certificate found
at sun.security.validator.Sim pleValidat or.buildTr ustedChain (SimpleVal idator.jav a:304)
at sun.security.validator.Sim pleValidat or.engineV alidate(Si mpleValida tor.java:1 07)
at sun.security.validator.Val idator.val idate(Vali dator.java :202)
at com.sun.net.ssl.internal.s sl.X509Tru stManagerI mpl.checkS erverTrust ed(DashoA1 2275)
at com.sun.net.ssl.internal.s sl.JsseX50 9TrustMana ger.checkS erverTrust ed(DashoA1 2275)
javax.net.ssl.SSLHandshake
te found
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at sun.net.www.protocol.https.HttpsClient.afterConnect(DashoA12275)
error [Ljava.lang.StackTraceElem
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA12275)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(DashoA12275)
at com.uprr.app.ena3.endpoint
Caused by: sun.security.validator.Val
at sun.security.validator.Sim
at sun.security.validator.Sim
at sun.security.validator.Val
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
HttpsURLConnection extends HttpURLConnection with support for https-specific features.