Sparkles

that were shone when I got tempered!

Send an SSL request in java

leave a comment »

public Request sendRequest(Request paymentrequest) throws Exception {

// Create a trust manager that does not validate certificate chains
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[] {
new javax.net.ssl.X509TrustManager(){

public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
}
};
 // Install the all-trusting trust manager
try {
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore

URL url = new URL(paymentrequest.getMConnectUrl());

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);

conn.setRequestMethod("POST");

if (isAuthNeeded) {

String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); // username:password

conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setRequestProperty("Content-Type", "text/xml");

}

Enumeration keys = requestProp.keys();

while (keys.hasMoreElements()) {
String key = keys.nextElement().toString();
conn.setRequestProperty(key, requestProp.getProperty(key));
}

// Send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paymentrequest.getRequestString());
wr.flush();
wr.close();

conn.connect();

/* Reading the inputStream */
InputStream _newInputStr = conn.getInputStream();
byte[] barr = new byte[1024 * 512];
int b = 0;
int i = 0;

while ((b = (int) _newInputStr.read()) > -1) {
barr[i] = (byte) b;
i += 1;
}

String resstr = new String(barr).trim();

paymentrequest.setResponseString(resstr);

return paymentrequest;
}

Advertisements

Written by Namal Fernando

July 29, 2013 at 3:01 pm

Posted in Java, SSL

Tagged with ,

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

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: