HTTP 400响应Servlet

时间:2014-07-07 15:45:24

标签: java servlets

以下代码是servlet的一部分,它将获取cookie值并将请求发送到另一个具有相同cookie值的服务以及其他标头。 我正在responseCode = serviceUrlConnection.getResponseCode();is = serviceUrlConnection.getInputStream();上收到HTTP 400响应。

使用相同的输入值(cookie和其他标头),我可以使用SOAP UI从服务获得正确的输出。有人可以指出错误。

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

2 个答案:

答案 0 :(得分:1)

错误400表示您的请求有问题。要检查的一些事项:

  • 服务器真的期待GET请求,而不是POST吗?要发帖,您可以致电serviceUrlConnection.setRequestMethod("POST")
  • 您是设置setDoOutput(true)但未在请求中写入任何内容。也许你需要写一些内容。

答案 1 :(得分:0)

默认情况下,请求方法是GET。因此,如果无需发送数据,我们无需设置DataOutputStream,也无需拨打setDoOutput(true)

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

查看现有的SO问题: -

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