Android 如何在改型中重定向时包含标题
我尝试为android应用程序制作oauth2。它有一个小虫子 我的错误是,当我重定向时,它没有类似头的授权Android 如何在改型中重定向时包含标题,android,redirect,retrofit2,oauth2,okhttp3,Android,Redirect,Retrofit2,Oauth2,Okhttp3,我尝试为android应用程序制作oauth2。它有一个小虫子 我的错误是,当我重定向时,它没有类似头的授权 public static Retrofit getLoginRetrofitOnAuthz() { Retrofit.Builder builder = new Retrofit.Builder().baseUrl(ServerValue.AuthServerUrl).addConverterFactory(GsonConverterFactory.create());
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;
}
mycokiecode。它在我登录时发送授权。但当我重定向时,它不起作用
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;
}
服务器结果(顶部登录、底部登录、重定向)
你知道如何在重定向时保留标题吗?事实上,罪人是OkHttp,但不是改型。 OkHttp故意删除所有身份验证头: 以下是对这个问题的讨论: 您可以使用OkHttp身份验证器。如果返回401错误,将调用它。因此,您可以使用它来重新验证请求
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禁止而不是401。我必须去
response.headers().get(“位置”)代码>
就位并创建和触发另一个网络请求:
public Call getMoreBills(@Header(“Authorization”)String Authorization,@Url String nextPage)
事实上罪人是OkHttp,但不是改型。
OkHttp故意删除所有身份验证头:
以下是对这个问题的讨论:
您可以使用OkHttp身份验证器。如果返回401错误,将调用它。因此,您可以使用它来重新验证请求
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禁止而不是401。我必须去
response.headers().get(“位置”)代码>
就位并创建和触发另一个网络请求:
public Call getMoreBills(@Header(“Authorization”)String Authorization,@Url String nextPage)
在我的例子中,服务器在重定向后返回了200。如果代码是成功代码,如何发出另一个请求?在我的例子中,服务器在重定向后返回了200。如果代码是成功代码,如何发出另一个请求?