我使用以下代码将JSON请求发送到我的Web服务,对于错误的请求,我返回了400消息代码以及有效负载中的详细JSON错误响应。
我不明白在使用时如何在客户端上检索此有效负载HttpURLConnection,因为它会立即引发IOException和连接InputStream是null。
HttpURLConnection connection = this.connect(url);
connection.setRequestMethod(method);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonMessage);
InputStream is = null;
try {
is = connection.getInputStream(); // IOException is thrown, how can I still get payload??
} catch (Exception e) {
}
String response = getStringFromInputStream(is);
Run Code Online (Sandbox Code Playgroud)
也尝试使用getErrorStream(),但这包含Apache Tomcat的默认错误页面,而不是我在例如使用可视REST API客户端时看到的有效负载。