OkHttpClient设置请求响应时长和请求重试次数

依赖jar包

 <dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.6.0</version>
</dependency>

代码


import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.httpclient.HttpException;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author hekunming
 * @Date 2021/12/14 9:34
 */
public class HttpResponseConnectTimeout {

    public static void main(String[] args) throws HttpException, IOException {
        //请求地址
        String url = "";
        //请求重试次数(本次+ 1)=2次
        //超时时间(次/秒)2秒
        System.out.println(httpRequest(url,1,2));
    }


    /**
     * 设置请求响应时长和请求重试次数
     * @param url 请求地址
     * @param maxRentry 请求重试次数(本次+ maxRentry)
     * @param timeout 超时时间(次/秒)
     * @return
     */
    public static String httpRequest(String url,  int maxRentry,int timeout ) {
        //请求响应结果
        String responseResult;
        //响应
        Response response = null;
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(timeout, TimeUnit.SECONDS)
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .addInterceptor(new HttpResponseConnectTimeout.OkhttpInterceptor(maxRentry)) //过滤器,设置最大重试次数
                .retryOnConnectionFailure(false) //不自动重连
                .build();
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .build();
        try {
            response = client.newCall(request).execute();
            if (response != null) {
                responseResult = response.body().string();
                response.close(); // 关闭连接
                return responseResult;
            } else {
                System.out.println("接口请求超时");
                return null;
            }
            /*如果response可以正常返回,说明能连接上,接着写业务逻辑即可*/
        } catch (Exception e) {
            //捕获异常,intercept must not return null.过滤器不能返回null
            System.out.println("重试2次后,访问超时.");
            return null;
        }

    }


    public static class OkhttpInterceptor implements Interceptor {
        // 最大重试次数
        private int maxRentry;

        public OkhttpInterceptor(int maxRentry) {
            this.maxRentry = maxRentry;
        }

        @NotNull
        @Override
        public Response intercept(@NotNull Chain chain) throws IOException {
            /* 递归 2次下发请求,如果仍然失败 则返回 null ,但是 intercept must not return null.
             * 返回 null 会报 IllegalStateException 异常
             * */
            return retry(chain, 0);//这个递归真的很舒服
        }

        Response retry(Chain chain, int retryCent) {
            Request request = chain.request();
            Response response = null;
            try {
//                System.out.println("第" + (retryCent + 1) + "次执行发http请求.");
                response = chain.proceed(request);
            } catch (Exception e) {
                if (maxRentry > retryCent) {
                    response = retry(chain, retryCent + 1);
                }
            } finally {
                return response;
            }
        }
    }


}



  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值