我在发送请求并使用代码401获取响应时遇到此错误:
com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found
有人说:
发生此错误的原因是服务器发送401(未授权)但未提供“WWW-Authenticate”,这是客户端下一步操作的提示. “WWW-Authenticate”标题告诉客户端需要哪种认证(基本或摘要).这在无头http客户端中通常不是很有用,但这就是标准的定义方式.发生此错误是因为lib尝试解析“WWW-Authenticate”标头但不能解析.
(android – volley error No authentication challenges found)
但这对我来说很奇怪,因为我不想使用WWW认证的东西,我只想获得代码401,但我总是得到例外.
我该如何绕过这个问题?任何建议都非常感谢.
解决方法:
我做了一些研究并得出结论,这是一个服务器问题,不遵循惯例.
来自维基:
401 Unauthorized (RFC 7235)
Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.
我认为解决这个问题的最好方法是通过添加标题在服务器中解决(类似于:
WWW-Authenticate: xBasic realm=””
对我来说,我无法更改服务器,因此我必须检查错误消息以检测401错误:
if (error.getMessage().equalsIgnoreCase("java.io.IOException: No authentication challenges found")){
showLoginError();
}
不是很优雅的解决方案,但现在工作.
标签:java,android,android-volley
来源: https://codeday.me/bug/20190528/1170368.html