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[] trustAllCerts = new[] {

public[] getAcceptedIssuers() {
return null;

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

public void checkServerTrusted([] certs, String authType) { }
 // Install the all-trusting trust manager
try { sc ="SSL");
sc.init(null, trustAllCerts, new;;
} 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();


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());


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

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

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


return paymentrequest;


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


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.


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


that were shone when I got tempered!

%d bloggers like this: