Monday, April 2, 2012

Create HTTPS Web Service Client

HTTPS web services uses certificate authentication. So, web service clients must be created with SSL.

In netbeans, you can create HTTPS web service client. Follow the steps.

1) Create Java project in Netbeans
2) Copy certificate into project root directory
3) Use Netbeans Web Service Client wizard to create client 

This will create web service clients successfully. But, when calling service, it is again needed to create SSL session with service.

Before calling service, certificate needs to be stored in java key store. 

Refer article on seguide :- http://seguide.blogspot.com/2009/12/use-keytool-to-generate-keys-in-java.html

Use following code before calling service methods.

System.setProperty("javax.net.ssl.trustStore", "export/home/myTrustStore"); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); 
System.setProperty("javax.net.ssl.trustStoreType", "JKS"); System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

URL url = new URL("https://~~~~~~~~~~~~~~~~~~~/service.asmx?wsdl");
Service service = new Service(url); service.getServiceSoap().getData();