小编use*_*796的帖子

HttpURLConnection:如何读取400响应的有效负载

我使用以下代码将JSON请求发送到我的Web服务,对于错误的请求,我返回了400消息代码以及有效负载中的详细JSON错误响应。

我不明白在使用时如何在客户端上检索此有效负载HttpURLConnection,因为它会立即引发IOException和连接InputStreamnull

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客户端时看到的有效负载。

java json httpurlconnection ioexception payload

5
推荐指数
1
解决办法
8476
查看次数

标签 统计

httpurlconnection ×1

ioexception ×1

java ×1

json ×1

payload ×1