在 java 中避免对 http 调用进行 URI 验证

Avoid URI validation for http call in java

我有 http 调用,其中查询参数应包含 JSON 以及 { } [ ] not编码。

Url 示例:

http://server.com?queryParam={%22address1%22%3A%2277+tehama%22%2C%}

在这种情况下 { } 应该在没有编码的情况下传递。

我知道根据 URL 编码参考 这个符号应该被编码但是它是第三方 API 并且它不能在任何地方改变方法。

到目前为止,我已经尝试通过以下方式进行此类调用:

所有这些客户端都有 URI 验证,不允许进行此类调用。 Spring 客户端将 {*} 视为参数名称并失败并出现异常,java HttpClient 失败并出现 java.lang.IllegalArgumentException: Illegal character in query.

有没有办法禁用对提到的客户端或其他一些允许此类调用的 http 客户端(可能是低级别)的验证?

找到了实现此目标的方法 java.net.URL

代码示例:

final URL url = new URL("http://server.com?queryParam={%22address1%22%3A%2277+tehama%22%2C%}");
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");

final Response response;
try (InputStream inputStream = connection.getInputStream()) {
   response = objectMapper.readValue(inputStream, Response.class);
} catch (final Exception e) {
   log.error("Error when calling API", e);
}