[英]okhttp3 gives a Protocol Exception error - Too many follow-up requests: 21
[英]Retrofit - Too many follow-up requests: 21
我正在使用 retrofit 發出請求。
我有以下錯誤:
java.net.ProtocolException:太多后續請求:21
代碼如下:
private OkHttpClient httpClient;
private CookieManager cookieManager;
public <S> S createCookieService(Class<S> serviceClass) {
httpClient.interceptors().clear();
httpClient.setCookieHandler(cookieManager);
Retrofit.Builder builder = new Retrofit
.Builder()
.client(httpClient)
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
然后我提出請求:
例子:
1) 登錄
@POST("/login")
Call<User> login();
2)一些要求:
@GET("/request")
Call<PojoPojo> getPojo();
而且我收到這個錯誤太多后續請求:21。
請幫忙。
傑克沃頓寫道:
當調用端點時有超過 20 個重定向時,這會被拋出(由 OkHttp,而不是 Retrofit)。 通常這表示兩個端點之間的重定向循環。 Chrome 和 Firefox 也將在這么多次重定向后停止加載請求並使請求失敗。
您需要咨詢您的服務器團隊或端點文檔,以確保將正確的數據直接傳遞到要調用的端點。 Retrofit 無需在此處執行任何操作。
其余的線程在那里: https : //github.com/square/retrofit/issues/1561
對我來說,問題是:請求 url 以"/"
開頭。
將 url @GET("/request")
替換為@GET("request")
"/"
結尾Authorization
標頭,請檢查是否需要將該值設置為"Bearer " + token
使用改造 2.4.0 版本:
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.4.0</version>
</dependency>
作為解決方法,可以使用:
new OkHttpClient.Builder() .followRedirects(FALSE) .followSslRedirects(FALSE)
很高興查看okhttp3.internal.http.RetryAndFollowUpInterceptor類源代碼。
我正在使用OkHttp的 3.12.0 版。
當令牌的標頭的鍵已經存在而不是用新令牌替換它時,也會發生這種情況,如下所示:
response.request.newBuilder()
.header("Authorization", token)
.build()
您添加新令牌(單個鍵可以在標頭中包含多個值),很多問題中常見的是添加這樣的令牌:
response.request.newBuilder()
.addHeader("Authorization", token)
.build()
這將導致請求再次失敗(401)並且您最終會陷入循環。
TL;DR:設置新令牌的正確方法是:
response.request.newBuilder()
.header("Authorization", token)
.build()
在我的情況下,這是因為標題中的 cookie 已過期。 很明顯,我們需要在服務器端代碼中修復此問題,但如果我們無法更改服務器端代碼,我們可以使用此解決方法。
以下是對我有用的解決方法
catch (ProtocolException e) {
Timber.w(e);
if (e.getMessage().contains("Too many follow-up requests")) {
doSynchronousLogin();
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("Cookie", dataManager.getCookie());
Timber.w("Request processed after auto-login");
return chain.proceed(builder.build());
}
}
以上代碼添加到改造攔截器中
在 okHttpClient 中,如果您使用身份驗證器,請在攔截器中刪除該身份驗證器。 它設置用於響應來自源服務器的挑戰的身份驗證器。 它使用 [proxyAuthenticator] 設置代理服務器的身份驗證器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.