[英]HttpURLConnection returns response as FileNotFoundException (404) instead of 401
My application uses java.net.HttpURLConnection
to get a file input stream. 我的应用程序使用
java.net.HttpURLConnection
来获取文件输入流。 Server is using spring boot framework of java. 服务器正在使用Java的Spring Boot框架。 When authentication token is expired spring boot filter is throwing SecurityException & we are setting response code as 401 as follows (Spring boot server side ):
当身份验证令牌过期时,Spring Boot过滤器将引发SecurityException&我们将响应代码设置为401,如下所示(Spring Boot服务器端):
catch (ExpiredJwtException eje) {
((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.UNAUTHORIZED);
}
But at android side we are getting exception FileNotFoundException which represents the 404 & response code as -1. 但是在android方面,我们得到了异常FileNotFoundException,它表示404和响应代码为-1。 Why am I not able to get the response code as 401 ?
为什么我无法获得响应代码401?
Following code I am using to check the response code (Android side): 我正在使用以下代码检查响应代码(Android端):
urlConnection.connect();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
}
InputStream inputStream = urlConnection.getInputStream();
Try this code. 试试这个代码。
urlConnection.connect();
InputStream inputStream = null;
if(urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
}else
{
inputStream = urlConnection.getErrorStream()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.