• Httpclient 4.5.2 请求重试机制


    重点是HttpRequestRetryHandler.retryRequest()方法

    public static String callHttpServer(String contentType,String json, String url) {
            String result = "";
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse response = null;
            try {
                
                HttpClientBuilder httpClientBuilder = HttpClients.custom();
                httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
                    @Override
                    public boolean retryRequest(IOException arg0, int retryTimes, HttpContext arg2) {
                        if (retryTimes > 3) {
                            return false;
                        }
                        if (arg0 instanceof UnknownHostException || arg0 instanceof ConnectTimeoutException
                                || !(arg0 instanceof SSLException) || arg0 instanceof NoHttpResponseException) {
                            return true;
                        }
    
                        HttpClientContext clientContext = HttpClientContext.adapt(arg2);
                        HttpRequest request = clientContext.getRequest();
                        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                        if (idempotent) {
                            return true;
                        }
                        return false;
                    }
                });
                httpclient = httpClientBuilder.build();
                HttpPost httppost = new HttpPost(url);
    
                httppost.setHeader("Content-Type", contentType);
                httppost.setHeader("Expect", "100-continue");
                httppost.setHeader("Accept-Encoding", "gzip,deflate");
                httppost.setHeader("Connection", "Keep-Alive");
                //如果json 为null,会出现异常
                if (null != json) {
                    StringEntity stringEntity = new StringEntity(json, "utf-8");
                    stringEntity.setContentEncoding("UTF-8");
                    stringEntity.setContentType("application/json");
                    httppost.setEntity(stringEntity);
                }
    
                response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
    
                if (entity != null) {
                    int status = response.getStatusLine().getStatusCode();
                    if ((status >= 200 && status < 300)) {
                        result = EntityUtils.toString(entity);
                    } else {
                        result = null;
                        
                    }
                }
            } catch (Exception ex) {
                logger.error("call http service error:{}", ex);
                result = null;
                ex.printStackTrace();
            } finally {
                if (null != response) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        logger.error("call http service response close error:{}", e);
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }

    更多参考: https://blog.csdn.net/zmx729618/article/details/78352879

  • 相关阅读:
    FreeSWITCH第三方库(视频)的简单介绍(二)
    FreeSWITCH第三方库(音频)的简单介绍(一)
    libreoffice实现WORD文档转PDF文档
    Linux TOP命令详解
    java内存泄漏
    FreeSWITCH的传真发送
    CPU的一些参数和排名
    设计模式(九)访问者模式
    设计模式(八)状态模式
    使用PermissionsDispatcher轻松解决Android权限问题
  • 原文地址:https://www.cnblogs.com/gczmn/p/11995674.html
Copyright © 2020-2023  润新知