简体   繁体   English

HttpURLConnection以FileNotFoundException(404)而不是401返回响应

[英]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.

相关问题 HttpURLConnection返回-1作为响应代码 - HttpURLConnection returns -1 as a response code Spring 启动:POST 请求返回 401 HttpUrlConnection - Spring Boot : POST request returns 401 HttpUrlConnection HttpUrlConnection.getInputStream 上的 HTTP 响应代码 401 - HTTP Response code 401 on HttpUrlConnection.getInputStream Android HTTPUrlConnection响应返回垃圾 - Android HTTPUrlConnection response returns garbage MockRestServiceServer 返回 404 状态而不是模拟外部服务响应 - MockRestServiceServer returns 404 status instead of mocking external service response Spring Boot,Spring Security 返回状态 401 而不是 404,用于“未找到 HTTP 请求的映射” - Spring Boot, Spring Security returns a status 401 instead of 404 for "no mapping found for HTTP request" Android HttpURLConnection总是未经授权的401响应(Spring Boot REST API) - Android HttpURLConnection always unauthorized 401 response (Spring Boot REST API) HttpURLConnection 使用自定义 header 发送 POST 的 401 响应代码 - 401 response code for HttpURLConnection sending POST with custom header Java - HttpUrlConnection 每次都返回缓存的响应 - Java - HttpUrlConnection returns cached response every time 什么是对此REST端点的404或401响应 - What is appropriate response a 404 or a 401 for this REST endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM