Java HTTP/HTTPs Client Example – Ignore SSL

January 3, 2013 at 10:19 am 1 comment

A HTTP Client example to ignore the SSL certificates.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
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;

public class HttpUtil {
static {
try {
TrustManager[] trustAllCerts = { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}

public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}
} };
SSLContext sc = SSLContext.getInstance("SSL");

HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
sc.init(null, trustAllCerts, new SecureRandom());

HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception localException) {
}
}

private static HttpUtil httpUtil;

private HttpUtil() {
}

public static HttpUtil getHttpUtil() {
if (httpUtil == null)
httpUtil = new HttpUtil();

return httpUtil;
}

public String doPost(String url, String data) throws Exception {
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.addRequestProperty("Content-Type", "application/xml");
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());

writer.write(data);
writer.flush();

String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
writer.close();
reader.close();
int status = conn.getResponseCode();
conn.disconnect();
return status + "";
}

public String doGet(String url) throws Exception {

// configure the SSLContext with a TrustManager

URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);

String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
conn.disconnect();

return buffer.toString();
}

public static void main(String[] args) {
try {
HttpUtil.getHttpUtil().doPost("url", null);
HttpUtil.getHttpUtil().doGet("url");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Use this one, if you dont have security issue. If you really want a security, then modify the above one by accepting the certificates.

About these ads

Entry filed under: HttpClient, Java, Restful Webservice. Tags: , , , , , , .

Connection Pooling : What, Why, How ?

1 Comment Add your own

  • 1. abhishek  |  January 22, 2013 at 6:47 pm

    Thanks karthik for a very helpful blog

    Reply

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

Trackback this post  |  Subscribe to the comments via RSS Feed


Blog Stats

  • 17,827 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: