【问题标题】:java.io.IOException: Server returned HTTP response code: 400 for URL:java.io.IOException:服务器返回 HTTP 响应代码:400 用于 URL:
【发布时间】:2018-11-01 16:39:06
【问题描述】:

我正在执行一个 GET 请求,并且此代码适用于我们所有现有的测试,它为新端点提供 400 错误。我可以在邮递员中很好地达到终点。 conn.getErrorStream() 不打印任何内容。代码似乎在 url.openConnection 处失败(没有抛出异常)。如果我在该行之后立即执行 conn.getResponseCode() 它返回 400。我想它可能不喜欢我在查询中的管道字符,而是通过 URLEncoder.encode(resourceContext) 以及 URLEncoder.encode(baseUrl + resourceContext) 没有帮助并最终抛出 MalformedURLException。这是我要访问的端点(匿名): https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA|415|415|CUST|GL53I7TPYBYM|MATCHED

当我将调试器附加到服务器时,我想什么都没有发生,因此请求永远不会到达服务器。我踏入 openConnection,它通向一个充满 var5、var6 等的兔子洞。我切换到 HttpsURLConnection,但那里也没有骰子。还有其他建议吗?

URL url = new URL(baseUrl + resourceContext);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod(requestType);
        if(messageLibraryJson) {
            conn.setRequestProperty("Content-Type", "application/json;class=true");
            conn.setRequestProperty("Accept", "application/json;class=true");
        } else {
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
        }

        String userpass = username + ":" + password;
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
        conn.setRequestProperty("Authorization", basicAuth);

        if(requestType.equals("POST")) {
            OutputStream os = conn.getOutputStream();
            os.write(requestJson.getBytes());
            os.flush();
        }

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            logger.error("ERROR in REST CLIENT");
            logger.error("URL = " + baseUrl + resourceContext);
            logger.error("Request = " + requestJson);
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

【问题讨论】:

    标签: java url


    【解决方案1】:

    确实是'|' char 造成麻烦。如果我执行以下操作:

            String urlString = (baseUrl + resourceContext).replace("|", "%7C");
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    

    它有效。生成以下 urlString: https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED

    但是,当我这样做时:

    String enc = baseUrl + URLEncoder.encode(resourceContext);
    

    我得到: https://clqa0003d.usa.company.com:17462/positions%2F1535000400000%2FCUR%2FUSA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED

    '/' 被 '%2F' 替换(除了管道字符)并且由于某种原因导致了麻烦。作为参考,端点实际上是这样定义的:

    @RequestMapping(value = "/{param1}/{param2}/{param3:.*}", method = RequestMethod.GET)
    

    类级别的端点(怎么叫???)为:

    @RequestMapping(value = "/positions",
        produces = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"},
        consumes = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"})
    public class PositionController implements IPositionController {
    

    谁能评论一下为什么将 '/' 转换为 '%2F' 会导致麻烦?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2011-04-21
      • 1970-01-01
      相关资源
      最近更新 更多