jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Apache HTTP Components SSL Request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Apache HTTP Components SSL Request" Watch "Apache HTTP Components SSL Request" New topic

Apache HTTP Components SSL Request

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 146

I am trying to upgrade the Apache HTTP Client code from version 3 to version 4. I am doing a http POST to a server.

The original version 3 code is:

I am trying to code it in version 4 which is:

But the error I am getting is:

javax.net.ssl.SSLException: hostname in certificate didn't match: <SnsServer.someCompanny.com/> != <SnsServer.someCompanny.com>

Can you help? Thanks
Andrea Paolo Ferraresi

Joined: Jun 29, 2012
Posts: 1
this code should be what you are looking for

package fitlibrary.ws.client;

import java.security.KeyStore;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

public class RelaxedHttpClient {
public static HttpClient create() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);

SSLSocketFactory sf = new RelaxedSSLSocketFactory(trustStore);

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();

package fitlibrary.ws.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLSocketFactory;

public class RelaxedSSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");

public RelaxedSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {

TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// Do nothing

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// Do nothing

public X509Certificate[] getAcceptedIssuers() {
return null;

sslContext.init(null, new TrustManager[] { tm }, null);

public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);

public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();

I agree. Here's the link: http://aspose.com/file-tools
subject: Apache HTTP Components SSL Request
It's not a secret anymore!