title: Java web service over https - How to add a self-signed certificate into a client api?
body: <p>I have a "Hello World" web service created with axis2. I would like to write a client api which could use this service over <code>https</code> with a self-signed certificate. I have a self-signed certificate <code>myCertificate.cer</code> and a <code>keystore</code> containing it.</p>
  
  <p>Here is my client API :</p>
  
  <pre><code>public class MyApi{
  
  public Object callMyService(){
  
  Axis2TestStub stub = new Axis2TestStub(
  "https://localhost:8443/axis2/services/Axis2Test");
  
  System.setProperty("javax.net.ssl.trustStore",
  "src/main/resources/myKeystore.jks")
  System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
  
  Hello request = new Hello();
  request.setMot("World");
  HelloResponse response = stub.hello(request);
  Object mot = response.get_return();
  return mot;
  </code></pre>
  
  <p>It works but I would like to use <code>myCertificate.cer</code> and not a <code>keystore</code> containing it. Does someone know how to do that? I tried to override <code>https</code> protocol with no success :</p>
  
  <pre><code>HttpSecureProtocol myHttpsProtocol = new HttpSecureProtocol();
  myHttpsProtocol .setCheckHostname(false);
  myHttpsProtocol .addTrustMaterial(new TrustMaterial("myCertificate.cer"));
  Protocol customHttps = new Protocol("https", 
  (ProtocolSocketFactory) myHttpsProtocol , 8443);
  Protocol.registerProtocol("https", customHttps);
  </code></pre>