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