简体   繁体   English

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

[英]java.io.IOException: Server returned HTTP response code: 400 for URL:

I am doing a GET request and this code, which works for all of our existing tests, is giving 400 error for a new endpoint. 我正在执行GET请求,并且此代码适用于我们所有的现有测试,并且为新端点给出了400错误。 I can hit the endpoint fine in postman. 我可以在邮递员那里达到终点。 conn.getErrorStream() doesn't print out anything. conn.getErrorStream()不输出任何内容。 The code seems to fail (without throwing exception)right at url.openConnection. 代码似乎在url.openConnection上失败(没有引发异常)。 If I do conn.getResponseCode() right after that line it returns 400. I thought perhaps it wasn't liking the pipe char I have in my query but encoding it via URLEncoder.encode(resourceContext) as well as URLEncoder.encode(baseUrl + resourceContext) didn't help and ended up throwing MalformedURLException. 如果我在该行之后立即执行conn.getResponseCode(),它将返回400。我认为这可能不喜欢查询中的管道字符,而是通过URLEncoder.encode(resourceContext)以及URLEncoder.encode(baseUrl)对其进行编码+ resourceContext)没有帮助,最终抛出MalformedURLException。 Here is the endpoint I want to hit (anonymized): https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA|415|415|CUST|GL53I7TPYBYM|MATCHED 这是我要命中的端点(匿名): https ://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA|415|415|CUST|GL53I7TPYBYM|MATCHED

When I attach the debugger to the server I want to hit nothing happens so the request never makes it to the server. 当我将调试器连接到服务器时,我不想执行任何操作,因此请求永远不会到达服务器。 I step into openConnection which leads to a rabbit hole full of var5, var6 etc. I switched to HttpsURLConnection but no dice there either. 我进入openConnection,这会导致充满var5,var6等的兔子洞。我切换到HttpsURLConnection,但那里也没有骰子。 Any other suggestions? 还有其他建议吗?

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

Indeed it was the '|' 确实是“ |” char causing trouble. 炭引起麻烦。 If I do the following: 如果我执行以下操作:

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

It works. 有用。 The following urlString is produced: https://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED 产生以下urlString: https ://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED

However, when I do: 但是,当我这样做时:

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

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

The '/' is replaced by '%2F' (in addition to the pipe char) and for some reason that causes trouble. '/'被'%2F'代替(除了管道字符),并且由于某种原因会引起麻烦。 For reference, the endpoint is actually defined as such: 作为参考,端点实际上是这样定义的:

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

With the class level endpoint (how is that called???) as: 使用类级别的终结点(如何称呼??)为:

@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 {

Can anyone comment on why converting '/' to '%2F' causes trouble? 谁能评论为什么将'/'转换为'%2F'会引起麻烦?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400 - server returns java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回URL的HTTP响应代码:400:java.io.IOException - Server returned HTTP response code: 400 for URL : java.io.IOException java.io.IOException:服务器返回HTTP响应代码:400表示URL - java.io.IOException: Server returned HTTP response code: 400 for URL 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器返回的HTTP响应代码:URL的400 - java.io.IOException: Server returned HTTP response code: 400 for URL 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code java.io.IOException:HTTP响应代码:400用于URL - java.io.IOException: HTTP response code: 400 for URL java.io.IOException:服务器返回HTTP响应代码:502 - java.io.IOException: Server returned HTTP response code: 502
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM