org.apache.http.conn.ConnectTimeoutException: *****.com:443 timed out

本文介绍了一种使用Apache HttpClient实现的HTTP客户端池解决方案,通过配置连接超时时间和信任管理器来解决访问特定网站时出现的连接超时问题。包括SSL上下文初始化、信任管理器设置、SO_TIMEOUT和CONNECTION_TIMEOUT参数调整等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shiyong de shi  https 

org.apache.http.conn.ConnectTimeoutException: *****.com:443 timed out


solution


public class httpsclientspool{


    private static final SchemeRegistry schemeRegistry = new SchemeRegistry();
    private static final PoolingClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager(schemeRegistry);
    private static final HttpParams params = new BasicHttpParams();
    private static DefaultHttpClient client = new DefaultHttpClient(clientConnectionManager, params);

    private static X509TrustManager trustManager= new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {
        }
        public void checkServerTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    static {
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 29000);
        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("SSL");
        } catch (NoSuchAlgorithmException e) {
            logger.error(" ssl error " , e);
        }

        try {
            ctx.init(null, new TrustManager[] { trustManager }, null);
        } catch (KeyManagementException e) {
            logger.error(" trustManager " , e);
        }

        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", 443, ssf)) ;
        clientConnectionManager.setMaxTotal(200);
        clientConnectionManager.setDefaultMaxPerRoute(20);
    }



    protected String doPost(String url, List<NameValuePair> params) {
        try {

            logger.info("post to url=" + url);
            HttpPost httpPost = new HttpPost(url);
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(uefEntity);
            HttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (null != response && response.getEntity()!= null) {
                responseText = EntityUtils.toString(entity, "UTF-8");
            }
            logger.info("post result=" + responseText);
            if (StringUtils.isBlank(responseText)) {
                throw new RuntimeException("返回为空!");
            }
        } catch (SocketTimeoutException timeoutException) {
               responseText = "";
           
        }catch (ConnectTimeoutException timeOut){
         
            responseText = "";
        } catch (Exception e) {
           
        }finally{
            
        }

        return responseText;
    }



done ~~



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值