The below code is part of a servlet which is taking the cookie value and sending request to another service with the same cookie value with additional headers.
I am getting HTTP 400 response on the responseCode = serviceUrlConnection.getResponseCode();
and on is = serviceUrlConnection.getInputStream();
.
With the same input values (cookie and additional headers), I able to get correct output from the service using SOAP UI. Could somebody point out the mistake.
URL serviceURL = new URL(serviceUrlInput);
logger.info(" Validate Test token service Url" + serviceUrlInput);
URLConnection serviceConnection = serviceURL.openConnection();
HttpURLConnection serviceUrlConnection = (HttpURLConnection)serviceConnection;
serviceUrlConnection.setRequestProperty("Content-Type", "application/json");
serviceUrlConnection.setRequestProperty("charset", "UTF-8");
String TestCookieValue = null;
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("Test")) {
//TestToken = cookies[i].getValue();
TestCookieValue = cookies[i].getValue();
logger.info("Test cookie : " + "Test=" +TestCookieValue);
//serviceUrlConnection.setRequestProperty("Cookie", TestCookie.substring(0, TestCookie.indexOf(';')));
serviceUrlConnection.setRequestProperty("Cookie", "Test=" +TestCookieValue);
break;
}
}
}
//Set the timestamp in the header
Date javaUtilDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String formattedDateTime = formatter.format(javaUtilDate);
serviceUrlConnection.setRequestProperty("timestamp", formattedDateTime);
logger.info(adapterDescription + " :: timestamp added with value :: " + formattedDateTime);
//Set the transactionId header
UUID uuid = java.util.UUID.randomUUID();
serviceUrlConnection.setRequestProperty("transactionId", uuid.toString());
logger.info(adapterDescription + " :: transactionID added with value :: " + uuid.toString());
//Set the sourceSystem header
String sourceSystem = + InetAddress.getLocalHost().getHostName();
serviceUrlConnection.setRequestProperty("sourceSystem", sourceSystem);
logger.info(adapterDescription + " :: sourceSystem added with value :: " + sourceSystem);
int responseCode;
serviceUrlConnection.setDoOutput(true);
wr = new DataOutputStream(serviceUrlConnection.getOutputStream());
wr.writeBytes("");
logger.info(adapterDescription +" :: " + wr);
responseCode = serviceUrlConnection.getResponseCode();
logger.info(adapterDescription +":: responseCode :: " + responseCode);
is = serviceUrlConnection.getInputStream();