当我在改造中重定向时如何包含 header
how to contain header when I redirect in retrofit
我试图为 android 应用程序制作 oauth2。它有一些小错误。
我的错误是当我重定向时它没有 header 之类的授权
我的Cookie代码。它在我登录时发送授权。但是当我重定向时它不起作用
public static Retrofit getLoginRetrofitOnAuthz() {
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(ServerValue.AuthServerUrl).addConverterFactory(GsonConverterFactory.create());
if (LoginRetrofitAuthz == null) {
httpClientAuthz.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
String str = etUsername.getText().toString() + ":" + etPassword.getText().toString();
String Base64Str = "Basic " + Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
System.out.println(Base64Str);
Request request = chain.request().newBuilder().addHeader("Authorization", Base64Str).build();
return chain.proceed(request);
}
});
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
httpClientAuthz.cookieJar(new JavaNetCookieJar(cookieManager));
LoginRetrofitAuthz = builder.client(httpClientAuthz.build()).build();
}
return LoginRetrofitAuthz;
}
服务器结果(Top-Login,底部,重定向)
你知道如何保持 header 重定向吗?
其实罪魁祸首是OkHttp,而不是Retrofit。
OkHttp 故意删除所有 身份验证 headers:
// When redirecting across hosts, drop all authentication headers. This
// is potentially annoying to the application layer since they have no
// way to retain them.
if (!sameConnection(url)) {
requestBuilder.removeHeader("Authorization");
}
这里是对这个问题的讨论:https://github.com/square/retrofit/issues/977
您可以使用 OkHttp 验证器。如果返回 401 错误,它将被调用。所以你可以用它来 re-authenticate 请求。
httpClient.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
return response.request().newBuilder()
.header("Authorization", "Token " + DataManager.getInstance().getPreferencesManager().getAuthToken())
.build();
}
});
但是在我的情况下,服务器返回 403 Forbidden 而不是 401。我必须得到
response.headers().get("Location");
in-place 并创建并触发另一个网络请求:
public Call<Response> getMoreBills(@Header("Authorization") String authorization, @Url String nextPage)
我试图为 android 应用程序制作 oauth2。它有一些小错误。
我的错误是当我重定向时它没有 header 之类的授权
我的Cookie代码。它在我登录时发送授权。但是当我重定向时它不起作用
public static Retrofit getLoginRetrofitOnAuthz() {
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(ServerValue.AuthServerUrl).addConverterFactory(GsonConverterFactory.create());
if (LoginRetrofitAuthz == null) {
httpClientAuthz.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
String str = etUsername.getText().toString() + ":" + etPassword.getText().toString();
String Base64Str = "Basic " + Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
System.out.println(Base64Str);
Request request = chain.request().newBuilder().addHeader("Authorization", Base64Str).build();
return chain.proceed(request);
}
});
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
httpClientAuthz.cookieJar(new JavaNetCookieJar(cookieManager));
LoginRetrofitAuthz = builder.client(httpClientAuthz.build()).build();
}
return LoginRetrofitAuthz;
}
服务器结果(Top-Login,底部,重定向)
你知道如何保持 header 重定向吗?
其实罪魁祸首是OkHttp,而不是Retrofit。 OkHttp 故意删除所有 身份验证 headers:
// When redirecting across hosts, drop all authentication headers. This
// is potentially annoying to the application layer since they have no
// way to retain them.
if (!sameConnection(url)) {
requestBuilder.removeHeader("Authorization");
}
这里是对这个问题的讨论:https://github.com/square/retrofit/issues/977
您可以使用 OkHttp 验证器。如果返回 401 错误,它将被调用。所以你可以用它来 re-authenticate 请求。
httpClient.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
return response.request().newBuilder()
.header("Authorization", "Token " + DataManager.getInstance().getPreferencesManager().getAuthToken())
.build();
}
});
但是在我的情况下,服务器返回 403 Forbidden 而不是 401。我必须得到
response.headers().get("Location");
in-place 并创建并触发另一个网络请求:
public Call<Response> getMoreBills(@Header("Authorization") String authorization, @Url String nextPage)