[英]How to get the response json on HTTP error 400 in Java
I'm making a request using HttpUrlConnection (POST):我正在使用 HttpUrlConnection (POST) 发出请求:
try
{
String v_Authorization="";
String v_MessageResponseCode="";
switch (in_TipoAuthorization) {
case 1: //No Auth
v_Authorization = in_Authorization;
break;
case 2: //Basic Auth
//...
break;
case 3: //Bearer Token
//...
break;
case 4: //OAuth2 Bearer Token
//...
break;
}
URL url = new URL(in_URL);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", v_Authorization);
conn.setRequestProperty("Content-Type", in_ContentType);
conn.setRequestProperty("Aceept", in_Accept);
conn.setRequestMethod(in_Method);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
os.write(in_JsonFile.getBytes("UTF-8"));
os.flush();
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder builderRetorno = new StringBuilder();
String retorno="";
while ((retorno = br.readLine()) != null) {
if (in_ImportCsvFile == 1) {
builderRetorno.append(retorno + "\n");
} else {
builderRetorno.append(retorno);
}
}
out_HTTP = builderRetorno.toString();
out_getResponseCode = conn.getResponseCode();
} catch(MalformedURLException ex){
logInfo(ex.toString());
ex.printStackTrace();
out_HTTP = ex.toString();
} catch(IOException e) {
logInfo(e.toString());
e.printStackTrace();
out_HTTP = e.toString();
} finally {
conn.disconnect();
}
When an error occurs I only have a Bad Request 400 return and e.printStackTrace does not tell me exactly what the error is:当发生错误时,我只有一个错误的请求 400 返回,而 e.printStackTrace 并没有告诉我确切的错误是什么:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:83)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:437)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1956)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1951)
at java.security.AccessController.doPrivileged(AccessController.java:703)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1950)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1520)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
at com.ibm.net.ssl.www2.protocol.https.b.getInputStream(b.java:91)
at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1504)
Caused by:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1906)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:491)
at com.ibm.net.ssl.www2.protocol.https.b.getResponseCode(b.java:56)
at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1493)
However, when I make the same request via POSTMAN, it returns exactly what the error is, example:但是,当我通过 POSTMAN 发出相同的请求时,它会准确返回错误内容,例如:
{ "profile": { "email": "must be a valid email address" } } { "profile": { "email": "必须是有效的电子邮件地址" } }
enter image description here在此处输入图片说明
How do I get this same error information in my java code?如何在我的 java 代码中获得相同的错误信息?
Thanks.谢谢。
Recommend you use ok-http .推荐你使用ok-http 。
Here is some helpful link.这是一些有用的链接。
http post request (json data) http post 请求(json 数据)
get an error message from response从响应中获取错误消息
If you only want to use HttpsURLConnection
.Like @Smile says, use conn.getErrorStream()
如果你只想使用
HttpsURLConnection
。就像@Smile 说的,使用conn.getErrorStream()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.