How to disable WWW-Authenticate header check on 401 in HttpUrlConnection on Android?

I am working with a web service that returns 401 when my cookie expires, which results in an HttpUrlConnection error:

java.io.IOException: No authentication challenges found at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427) at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407) at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356) at ... 

I know this happens because the server does not return the WWW-Authenticate header, but it is out of my control. It's nice that the HttpUrlConnection does this for me, but it is a real world, and we do not always work with servers that perfectly follow the HTTP specification. I cannot move away from HttpUrlConnection due to other code dependencies, so how do I disable validation so that I can really use the answer?

+4
source share
1 answer

you can try and catch the error.

 try{ //code that causes error } catch(IOException ex){ if(ex.getMessage().equals("No authentication challenges found")){ //handle error } } 
0
source

Source: https://habr.com/ru/post/1441944/


All Articles