package com.zztrapp.zztrapp.http;


import com.zztrapp.zztrapp.utilcode.klogs.KLog;
import com.zztrapp.zztrapp.utilcode.util.LogUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

//处理重定向的拦截器
public class RedirectInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        int code = response.code();
        KLog.e("重定向地址code：", "code = " + response.code());
        KLog.e("重定向地址body：", "body = " + response.body().string());
        KLog.e("重定向地址message：", "message = " + response.message());
//        LogUtils.e("重定向地址：", "code = " + response.code());
        String location = response.headers().get("Location");
        LogUtils.e("重定向地址：", "location = " + location);
        if (code == 302) {
            //获取重定向的地址
//            String location = response.headers().get("Location");
//            LogUtils.e("重定向地址：", "location = " + location);
            //重新构建请求
            Request newRequest = request.newBuilder().url(location).build();
            response = chain.proceed(newRequest);
        }
        return response;
    }
}