I have gone through this answer OkHttp doesn't redirect POST requests when used with retrofit in order to redirect a post request, more specifically when I got 307 responses. I have created an Interceptor called RedirectURL in order to redirect the requests but it does not work.
private static Interceptor redirectRequest() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(chain.request());
if (response.code() == 307) {
Log.d(TAG, "intercept: is true"+response.code());
request = request.newBuilder()
.url(response.header("Location"))
.build();
response = chain.proceed(request);
}
return response;
}
};
}
OkkHttp
private OkHttpClient okHttpClient() {
RepositoryService repositoryService = new RepositoryService(context);
return new OkHttpClient.Builder()
// .cache(cache())
.followRedirects(true)
.followSslRedirects(true)
.connectTimeout(5, TimeUnit.MINUTES) // re-request if package is drop or TimeOut reach 60 seconds
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.addInterceptor(httpsLoggingInterceptor()) // used if network off OR on
.addInterceptor(redirectRequest())
.addNetworkInterceptor(repositoryService.networkInterceptor())
.build();
}
Your example is creating a new GET request without the body.
The latest OkHttp version should be automatically following a redirect with a 307 response.
https://github.com/square/okhttp/pull/5990/files
Are you testing with a recent version eg 4.9.1?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.