12

I'm using OkHttp 2.3 with basic authentication requests, according to OKHttp docs, it automatically retries unauthenticated requests, but whenever I provide invalid credentials, the request takes too much time and I get this exception in the end:

java.net.ProtocolException: Too many follow-up requests: 21

How can I prevent OkHttp from automatically retrying unauthenticated requests, and return 401 Unauthorized instead?

4

2 回答 2

15
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;
        }
    };
}

在我的 Authenticator 中,我只计算尝试次数 - 在 X 次尝试之后,我抛出一个异常。

于 2015-04-17T08:52:26.240 回答
2

Traxdata 答案的修改版本有效:

protected Authenticator getBasicAuth(final String username, final String password) {
    return new Authenticator() {
        private int mCounter = 0;

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            Log.d("OkHttp", "authenticate(Route route, Response response) | mCounter = " + mCounter);
            if (mCounter++ > 0) {
                Log.d("OkHttp", "authenticate(Route route, Response response) | I'll return null");
                return null;
            } else {
                Log.d("OkHttp", "authenticate(Route route, Response response) | This is first time, I'll try to authenticate");
                String credential = Credentials.basic(username, password);
                return response.request().newBuilder().header("Authorization", credential).build();
            }
        }
    };
}

然后你需要:

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.authenticator(getBasicAuth("username", "pass"));
retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(builder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

而已。

于 2016-08-26T09:39:51.990 回答