-1

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();
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
  • 1
    HTTP 400 is "bad request", so clearly your code is not doing something which whatever service URL you're invoking is wanting to have. On that basis I cannot agree with your claim that your code and SoapUI are generating the exact same request. Did you try using a HTTP sniffer tool / browser plugin to log both requests and check for differences? – Gimby Jul 07 '14 at 15:52

2 Answers2

1

Error 400 means that there's something wrong with your request. A few things to check:

  • Is the server really expecting a GET request, not a POST? To do a post you can call serviceUrlConnection.setRequestMethod("POST")
  • You are settings setDoOutput(true) but are not writing anything in the request. Maybe you need to write some content.
David Levesque
  • 22,181
  • 8
  • 67
  • 82
0

By default the request method is GET. So, If no data need to be sent over, we dont need to set the DataOutputStream and also no need to call setDoOutput(true)

/*
Commented out the below lines:- 
wr = new DataOutputStream(serviceUrlConnection.getOutputStream());
serviceUrlConnection.setDoOutput(true);
*/

See an exiting SO question :-

HttpURLConnection sends a POST request even though httpCon.setRequestMethod("GET"); is set

Community
  • 1
  • 1
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134