当前位置: 移动技术网 > IT编程>开发语言>Java > Httpclient 4.5.2 请求重试机制

Httpclient 4.5.2 请求重试机制

2019年12月06日  | 移动技术网IT编程  | 我要评论

重点是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;
    }

 

更多参考:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网