File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Establishing https Connection using java 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Establishing https Connection using java 6" Watch "Establishing https Connection using java 6" New topic
Author

Establishing https Connection using java 6

kart siri
Greenhorn

Joined: Jan 09, 2012
Posts: 11
Hi,
I am running java 6 and want to know how to establish an https connection.

Basically I am trying to connect an https url through a program running on the same machine.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You shall check the javax.net.ssl.HttpsURLConnection class and search code samples on how https connection is established using the class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You start with java.net.URL to get a URLConnection. For http URLs this is an instance of an unknown sub class of HttpURLConnection so you can cast it. For https URLs it should be an instance of an unknown sub class of HttpsURLConnection so you can cast it as well, or cast to HttpURLConnection if you don't need any methods of HttpsURLConnection.

Alternatively you can use a third party library like HttpComponents (previously HttpClient) or HtmlUnit.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kart siri
Greenhorn

Joined: Jan 09, 2012
Posts: 11
I have used URL.openConnection() to open an https connection, but i always get the below exception on execution of connection.getResponseCode()

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)

I have enabled HTTPS port on my tomcat and i have .keystore file in home directory.
I have used "keytool" to generate the .keystore file.
 
GeeCON Prague 2014
 
subject: Establishing https Connection using java 6