您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页HttpClient工具类PostMethod添加重试策略

HttpClient工具类PostMethod添加重试策略

来源:保捱科技网
public class HttpClientUtil {

    private static final Log logger = LogFactory.getLog(HttpClientUtil.class);
    public static String doPost(String url, Map<String, String> params) {
        logger.info("doPost.url=" +url+",params"+ JSON.toJSONString(params));
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type","application/json;charset=utf-8");
        method.addRequestHeader("Accept", "application/json;charset=utf-8");
        // 重试策略;如果Http出错,默认三次重试
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        method.setRequestEntity(new ByteArrayRequestEntity(JSON.toJSONBytes(params)));
        String response = "";
        try {
            HttpClient client = new HttpClient();
            client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                response = method.getResponseBodyAsString();
            }
            logger.info("doPost.response=" + response);
        } catch (Exception e) {
            logger.error("执行HTTP Post请求[[" + url + "]]时,发生异常!", e);
        } finally {
            method.releaseConnection();
        }
        return response;
    }

}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务