简体   繁体   English

如何通过 java 代码在响应不是 200 的调用后捕获错误响应正文和错误代码?

[英]How to capture error response body and error code in post call where the response is not 200 via java code?

I am writing the below code, but that is only capturing response body when response code is 200. For all other occasions it is failing.我正在编写以下代码,但这仅在响应代码为 200 时捕获响应主体。对于所有其他情况,它都失败了。

Picture - postman post call and response where response code is not equal to 200图片 - postman 发布呼叫和响应,其中响应代码不等于 200

For example when response code 409, the code is throwing error with例如,当响应代码 409 时,代码会抛出错误

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 409 for URL: http://fetch.product/v1/products/PD16798270/validate at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515) at com.Main.main(Main.java:58) Exception in thread "main" java.io.IOException: Server returned HTTP response code: 409 for URL: http://fetch.product/v1/products/PD16798270/validate at java.base/sun.net.www.protocol. http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515) at com.Main.main(Main.java:58)

Code I am using below -我在下面使用的代码 -

public static void main(String[] args) throws Exception {       
     
     String url = "http://fetch.product/v1/products/PD16798270/validate";    
     URL u = new URL(url);
    
     StringBuilder postData = new StringBuilder();
     byte[] postDataBytes = postData.toString().getBytes("UTF-8");
     
     HttpURLConnection conn = (HttpURLConnection)u.openConnection();
     conn.setRequestMethod("POST");      
     
     conn.setDoOutput(true);
     conn.getOutputStream().write(postDataBytes);
     
     Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
     StringBuilder sb = new StringBuilder();
     for (int c; (c = in.read()) >= 0;)
            sb.append((char)c);
     String response = sb.toString();
     System.out.println(response);
     
}

If the request returns an error response, you can read it from the connection's "error stream".如果请求返回错误响应,您可以从连接的“错误流”中读取它。 For example, this would read the body whether the request returned success or error:例如,这将读取请求是否返回成功或错误的正文:

InputStream input;
if (conn.getResponseCode() >= 400) {
    input = conn.getErrorStream();
} else {
    input = conn.getInputStream();
}

Reader in = new BufferedReader(new InputStreamReader(input, "UTF-8"));

暂无
暂无

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

相关问题 Java:Android:Retrofit 2响应代码为200,response.body为null - Java : Android : Retrofit 2 response code is 200, response.body is null 改造call.enqueue代码200,但response.body()为空 - Retrofit call.enqueue code 200, but response.body() is empty 如何处理带有错误响应 rest 模板的 200 状态码? - How to handle 200 status code with error response rest template? 带有正文的 RestAssured Post 调用会引发错误“java.lang.AssertionError:1 期望失败。 预期状态代码 &lt;200&gt; 但为 &lt;415&gt;。” - RestAssured Post call with body throws an error “java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <415>.” Retrofit 响应体 null 但状态码为 200 - Retrofit response body null but status code is 200 Java HTTPS POST 请求返回 200 的响应代码,而我知道它应该返回 302 的响应代码 - Java HTTPS POST Request Is Returning A Response Code Of 200 When I Know It Should Be Returning A Response Code Of 302 改造2-响应正文返回null(响应代码为200) - Retrofit 2 - response body returns null (response code is 200) Http Post的响应代码为415。如何使响应代码为200 - Http Post's response code is 415. How to make the response code to be 200 Bitstamp {“error”:“未找到API密钥”}代码响应:200 - Bitstamp {“error”: “API key not found”} Code Response: 200 与Volley一起下载给出错误:意外响应代码200 - Download with Volley gives error: Unexpected response code 200
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM