[英]How do I capture the json response of a POST request when the 401 Unauthorized error occurs with Java?
I have the following API:我有以下 API:
https://mrcheff.herokuapp.com/v1/api/usuarios/login https://mrcheff.herokuapp.com/v1/api/usuarios/login
It is used to perform authentication passing username and password as parameters in the request body and I need to get the json that returns when the informed user is incorrect or the informed password is incorrect!它用于在请求正文中通过用户名和密码作为参数执行身份验证,我需要获取当通知用户错误或通知密码错误时返回的 json!
In this case, by typing the incorrect user, I return the json:在这种情况下,通过输入不正确的用户,我返回 json:
"errors": "Usuario não registrado"
And by typing the wrong password I get the json return:通过输入错误的密码,我得到 json 返回:
"errors": "Senha inválida"
My request looks like this:我的请求如下所示:
try {
String linkApi = getLinkApi().getProperty("linkApi");
URL url = new URL(linkApi.concat("/api/usuarios/login"));
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Content-Type", "application/json");
con.setRequestMethod("POST");
con.setDoOutput(true);
login = new Login();
gson = new Gson();
login.setEmail("email@email.com");
login.setPassword("senha123");
String input = gson.toJson(login);
System.out.println(input);
System.out.println("");
OutputStream os = con.getOutputStream();
os.write(input.getBytes());
os.flush();
int responseCode = con.getResponseCode();
System.out.println("Código: " + responseCode);
System.out.println("");
BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
String output;
StringBuffer response = new StringBuffer();
while ((output = br.readLine()) != null) {
response.append(output);
}
br.close();
System.out.println(response.toString());
System.out.println("");
} catch(MalformedURLException e) {
System.out.println("Houve um erro. Exception: " + e.getMessage());
return false;
} catch (IOException e) {
System.out.println("Houve um erro. Exception: " + e.getMessage());
return false;
} finally {
con.disconnect();
}
Everything flows well until you get to that part of the code:一切顺利,直到您到达代码的那部分:
BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
Because arriving at this line, if the error is 401, it falls in the catch and returns me only that it gave the error 401.. how do I get the json's?因为到达这一行,如果错误是 401,它会被捕获并只返回给我它给出了错误 401 .. 我如何获得 json 的?
If the response code is less than 400 you can use getInputStream() otherwise you can use getErrorStream() instead of getInputStream().如果响应代码小于 400,则可以使用 getInputStream(),否则可以使用 getErrorStream() 而不是 getInputStream()。
BufferedReader br = null;
if (100 <= con.getResponseCode() && con.getResponseCode() < 400) {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, getErrorStream method will return null.如果连接没有连接,或者服务器在连接时没有错误,或者服务器有错误但没有发送错误数据,getErrorStream 方法将返回 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.