HTTPClient无法在https和http之间建立路由 [英] HTTPClient unable to establish route between https and http

查看:100
本文介绍了HTTPClient无法在https和http之间建立路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过击打http和https链接的混合物来测试HttpClient 4.2。

I am testing HttpClient 4.2 by hitting a mixture of http and https links.

HttpClient似乎从第一次调用开始就坚持使用该协议。如果第一个呼叫是http,则随后的所有https呼叫都会失败,但是http呼叫是可以的。反之亦然。

HttpClient seems to stick with the protocol from the first call. If the first call is http, then all following https calls fail but http calls are fine. And vice versa.

这是我使用的测试代码。

Here is the test code I used.

@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)将失败,但是百度的要求很好。

The second request (https) will fail, but the baidu request is fine.

原因:org.apache.http.HttpException:无法建立路由:计划= {s}-> https://www.hotmail.com ;当前= {s}-> http://www.hotmail.com org.apache上的
.http.impl.client.DefaultRequestDirector。EstablishmentRoute(DefaultRequestDirector.java:842)

Caused by: org.apache.http.HttpException: Unable to establish route: planned = {s}->https://www.hotmail.com; current = {s}->http://www.hotmail.com at 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 。两种情况下都会引发类似的错误。

I also have to disable redirection because hotmail redirects request: http://www.hotmail.com -> https://www.hotmail.com or https://www.hotmail.com -> https://www.live.com. A similar error is thrown in either cases.

包装器如下所示。

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;
        }
    }
}


推荐答案

HttpClient应该能够对用户绝对透明地管理连接。此问题很可能是由4.2版本中引入的回归引起的(请参阅HTTPCLIENT-1193 )。

HttpClient should be able to manage connections absolutely transparently to the user. This problem is likely to be caused by a regression introduced in the 4.2 release (see HTTPCLIENT-1193).

使用PoolingConnectionManager或SingleConnectionManager代替默认值,直到发布4.2.1版本。

Use either PoolingConnectionManager or SingleConnectionManager instead of the default one until 4.2.1 version is released.

这篇关于HTTPClient无法在https和http之间建立路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆