Sil*_*a H 10 authentication android http-status-code-401 okhttp okio
OkHttp 2.3根据OKHttp文档,我正在使用基本身份验证请求,它会自动重试未经身份验证的请求,但每当我提供无效凭据时,请求都会花费太多时间,最后我得到了这个异常:
java.net.ProtocolException:后续请求太多:21
如何阻止OkHttp自动重试未经身份验证的请求,然后返回401 Unauthorized?
小智 13
protected Authenticator getBasicAuth(final String username, final String password) {
return new Authenticator() {
private int mCounter = 0;
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
if (mCounter++ > 0) {
throw new AuthenticationException(
AuthenticationException.Type.INVALID_LOGIN, response.message());
}
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Authorization", credential).build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
};
}
Run Code Online (Sandbox Code Playgroud)
在我的身份验证器中,我只计算尝试次数 - 在X次尝试之后,我抛出异常.
| 归档时间: |
|
| 查看次数: |
6277 次 |
| 最近记录: |