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.
Entry filed under: HttpClient, Java, Restful Webservice. Tags: HttpClient, Httpclient with SSL, HttpsClient, java httpclient, restful httpclient, restful https client, SSL.
1.
abhishek | January 22, 2013 at 6:47 pm
Thanks karthik for a very helpful blog