我在应用程序中使用特定的身份验证机制。
登录应用程序将登录请求发送到特定的 url /login/ 并获取存储在 cookie 中的会话令牌。
服务器随时可以使令牌失效,其他请求将导致 401。我希望 HttpClient 自动检测 401 错误,重做 /login/ 请求一次并自动重试原始请求。
我尝试在 HttpClients.custom().setRetryHandler 中设置 HttpRequestRetryHandler 但它似乎无法处理 401 错误。
执行请求的功能的最佳方式是什么?
您可以根据需要使用 OkHttpClient。它完全实现了你想要的。
处理身份验证。OkHttp can automatically retry unauthenticated requests. When a response is 401 Not Authorized, an Authenticator is asked to supply credentials. Implementations should build a new request that includes the missing credentials. If no credentials are available, return null to skip the retry.
您可以阅读有关图书馆的更多信息here https://github.com/square/okhttp/wiki/Recipes.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)