RSS

Jersey Client with HTTPS problem

14 May

There is a client helper code : https://gist.github.com/1069465, It can help to figure out the problem of HTTPS connection from client side. 

In order to understand the logic, I copy the code here: 

  • ClientHelper.java

/*
* from https://gist.github.com/1069465
*/
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;

public class ClientHelper {

public static ClientConfig configureClient() {
TrustManager[ ] certs = new TrustManager[ ] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance(“TLS”);
ctx.init(null, certs, new SecureRandom());
} catch (java.security.GeneralSecurityException ex) {
}
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

ClientConfig config = new DefaultClientConfig();
try {
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
},
ctx
));
} catch(Exception e) {
}
return config;
}

public static Client createClient() {
return Client.create(ClientHelper.configureClient());
}

}

  • main .java

public static void main(String[] args) {
try {
ClientConfig config = ClientHelper.configureClient();
Client client = Client.create(config);
WebResource webResource = client.resource(“https:// <ip address> /……”);
ClientResponse response = webResource.accept(“application/json”).get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException(“Failed : HTTP error code : “
+ response.getStatus());
}

String output = response.getEntity(String.class);

System.out.println(“Output from Server …. \n”);
System.out.println(output);

} catch (Exception e) {

e.printStackTrace();

}

}

 

 

Advertisements
 
Leave a comment

Posted by on May 14, 2012 in Uncategorized

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: