java.io.IOException:服务器为URL返回HTTP响应代码:400

时间:2018-11-01 16:39:06

标签: java url

我正在执行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())));

2 个答案:

答案 0 :(得分:0)

实际上是“ |”炭引起麻烦。如果我执行以下操作:

        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'导致麻烦吗?

答案 1 :(得分:-1)

400的响应码表示服务器由于语法无效而无法处理客户端发送的请求。

我建议再次检查语法(您尝试发送到远程服务器的数据)

欢呼