Accessing SSL enabled Web Services

<!– @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } –>

This document will guide you in Consuming a Web Service over a HTTPS protocol.

I am using AXIS2 for deploying my Web Service and hence here I will present you with an example of making a client for a Web Service ( Version ) which comes deployed in AXIS2.

For security we usually deploy out Web Service over the HTTPS protocol. As you must be knowing HTTPS protocol uses Digital Certificate for the authentication and to ensure that no one else except the the authenticated parties may access/tamper the data.

Since in my previous post i:e: I described how to enable the JBOSS Application Server.

There we created a keystore ( which contains a certificate ).

Now we have to export the server certificate so that clients can use it. For exporting the certificate we will write

keytool ā€“ export -alias <keystore-alias-name> -keystore <keystore-name> -rfc -file <your-cert-name.cer>

then it will prompt for the password ( of the keystore ) : give it and your certificate will be generated by the name.

In my case I did :

keytool -export -alias nsdg_ssl -keystore nsdg.keystore -rfc -file nsdg_temp.cer

and a certificate by name nsdg_temp.cer was generated.

If you want to see the certificate you can write at the terminal :

cat you-cert-name.cer

If a client has to communicate to a server over SSL, it must have a truststore file. Below is given the command which will import the certificate to the client truststore :

keytool -import -alias <trust-store-name> -file <cert-file-name.cer> -keystore <client-trust-store>

FINALLY We will write a JAVA CLIENT which will call a web service.

To access a secure website from a web service client you need to set the following properties : and

The value of should be the path of your truststore file. You must have already imported the certificate to this truststore from th server to which you are going to communicate.

This is how i did :


System.setProperty(“”, “rmi+ssl”);

My Client looked like :

import javax.xml.namespace.QName;







import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.*;


* @author bhupesh


public class VersionJavaClient {

private EndpointReference targetEPR;

private QName operationName = new QName(“version”);

public static void main(String[] args) throws AxisFault {



System.setProperty(“”, “rmi+ssl”);

VersionJavaClient client = new VersionJavaClient();



private void getAxisVersion() throws AxisFault {

System.out.println(“getAxisVersion() STARTS”);

OMElement payload = createRequestMethod();

ServiceClient serviceClient = new ServiceClient();

Options options = new Options();

options.setTo(new EndpointReference(“;));



OMElement result = serviceClient.sendReceive(payload);

System.out.println(“getAxisVersion() ENDS”);

if (result != null) {

System.out.println(“RESULT WILL BE DISPLAYED NOW”);


} else {

System.out.println(“Got Null Result”);


private OMElement createRequestMethod() {

System.out.println(“createRequestMethod() :: creating envelope”);

OMFactory fac = OMAbstractFactory.getSOAP11Factory();

OMNamespace omNs = fac.createOMNamespace(“http://localhost/d2&#8221;, “d2”);

OMElement requestMethod = fac.createOMElement(“getVersion”, omNs);

OMElement firstElement = fac.createOMElement(“firstElement”, null);



System.out.println(“createRequestMethod() :: request method returned..”);

return requestMethod;


private void displayResult(OMElement result) {

System.out.println(“displayResult() :: STARTS”);

try {

XMLStreamWriter writer = XMLOutputFactory.newInstance()


//result.serialize(new OMOutputImpl(writer));


System.out.println(“trying to serialize the result. FLUSHING….”);

System.out.println(“displayResult() :: ENDS”);


} catch (Exception e) {

System.out.println(“got Exception while printing”);