了HTTPClient无法与HTTPS和HTTP建立航线(HTTPClient unable to

2019-07-29 12:49发布

我击中的http和https链接的混合测试的HttpClient 4.2。

HttpClient的似乎坚持第一次调用的协议。 如果第一个电话是HTTP,那么所有下面的HTTPS调用失败,但HTTP调用的罚款。 反之亦然。

下面是我使用的测试代码。

@Test
public void testNoRedirectMixed() throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient=WebClientDevWrapper.wrapClient(httpclient);
    HttpClientParams.setRedirecting(httpclient.getParams(), false);

    {
    HttpGet httpget = new HttpGet("http://www.hotmail.com");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    assertTrue(EntityUtils.toString(entity).indexOf("com")>0);
    }

    try {
    HttpGet httpget = new HttpGet("https://www.hotmail.com");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    }catch (Exception e) {
        e.printStackTrace();
    }

    {
    HttpGet httpget = new HttpGet("http://www.baidu.com");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    assertTrue(EntityUtils.toString(entity).indexOf("com")>0);
    }
}

第二请求(HTTPS)将失败,但百度请求是好的。

org.apache.http.HttpException::无法建立路由:通过引起计划= {S} - > https://www.hotmail.com ; 电流= {S} - > http://www.hotmail.com在org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:842)

我也有,因为hotmail的重定向请求停用重定向: http://www.hotmail.com - > https://www.hotmail.com或https://www.hotmail.com - > https://www.live .COM 。 类似的错误在任何情况下抛出。

该包装如下所示。 它是用来接受所有证书。

public class WebClientDevWrapper {

    public static HttpClient wrapClient(HttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] xcs,
                        String string) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] xcs,
                        String string) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[]{};
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
                ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = base.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", ssf, 443));
            DefaultHttpClient client= new DefaultHttpClient(ccm, base.getParams());
            return client;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Answer 1:

HttpClient的应该能够完全透明地管理用户的连接。 这个问题很可能是在4.2版本中引入的一个回归引起的( 参见HttpClient的-1193 )。

发布一个直到4.2.1版本请使用PoolingConnectionManager或SingleConnectionManager而不是默认的。



Answer 2:

您正在尝试使用一个连接进行通信,以许多不同的网站。 AFAIR你必须创建为每一个独特的网站新的连接(==新的客户端)。



文章来源: HTTPClient unable to establish route between https and http