JAX-WS “HTTPS hostname wrong: should be <mysite.com>” Exception

If the “cn” of the web services server certificate does not match its host name, you’ll get:
com.sun.xml.ws.client.ClientTransportException: HTTP transport error: java.io.IOException: HTTPS hostname wrong: should be <mysite.com>

You can implement your own javax.net.ssl.HostnameVerifier to suppress it. Important, remember to remove the codes for production!

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
 
public class TestHostnameVerifier implements HostnameVerifier {
	public boolean verify(String arg0, SSLSession arg1) {
		return true;
	}
}

In client code:

// Make sure java.protocol.handler.pkgs is set to "javax.net.ssl"
//System.setProperty( "java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol" );
System.setProperty( "java.protocol.handler.pkgs", "javax.net.ssl" );
Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
 
HelloService service = new HelloService();
HelloPort proxy = service.getHelloPort();
 
Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://new/endpointaddress");
ctxt.put(JAXWSProperties.HOSTNAME_VERIFIER, new TestHostnameVerifier());
 
proxy.sayHello("Hello World!");
This entry was posted in jax-ws, ssl. Bookmark the permalink.