How to send an SSL request to web service with Java based codes

You can compile this java codes to test an HTTPS service. At this post, I described briefly how to test an SSL encrypted web service with  SSLPoke.class and also compile a new java class. 

Create your java class to test connection

Step 1: Create a java class file

Open a new file which names "URLConnectionReader "  and add these lines. You can change  TLS version also other attributes. Please check java.net for more details about  System.setProperty parameters.

#vi  URLConnectionReader.java

import java.net.*;

import java.io.*;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {

System.setProperty("javax.net.debug", "all");
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.ssl.trustStore","C:/Program Files/Java/jdk1.7.0_72/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword","changeit");
java.lang.System.setProperty(
"jdk.tls.client.protocols", "TLSv1,TLSv1.1,TLSv1.2");
URL casesup = new URL(args[0]);
URLConnection yc = casesup.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Step 2: Compile  it  with  javac

#javac   URLConnectionReader.java

Step 3: Test  your Class

#java  URLConnectionReader  https://google.com.tr

Please don't forget to run this command under the directory where   URLConnectionReader.class  located.

Use SSL Poke to verify connectivity

 
Try the Java class SSLPoke to see if your truststore contains the right certificates. This will let you connect to an SSL service, send a byte of input, and watch the output.

Step 1: Download SSLPoke.class
You should download 
SSLPoke.class file from that like then execute the class as per the below, changing the URL and port appropriately.

Step 2: Check URL

#$JAVA_HOME/bin/java SSLPoke casesup.com 443

A failed connection would produce the below:

$JAVA_HOME/bin/java SSLPoke jira.example.com 443
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
	at sun.security.validator.Validator.validate(Validator.java:260)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1351)
	at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
	at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
	at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
	at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:728)
	at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
	at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)
	at SSLPoke.main(SSLPoke.java:31)
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
	at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
	... 15 more

 

You can check this link for more details. 

  

Tagged In:

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me