Apache HttpClient 5.0 HTTPS代理:“除非连接,否则没有隧道”

时间:2019-01-31 00:23:41

标签: java apache-httpclient-5.x

我正在使用最新版本的JDK8 通过执行以下操作来创建我的CloseableHttpAsyncClient

        try{
            sslContext = SSLContexts.custom()
                    .setProvider(Conscrypt.newProvider())
                    .build();
        }catch (Exception e){
            e.printStackTrace();
        }

        final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
                .setTlsStrategy(new ConscryptClientTlsStrategy(sslContext))
                .build();

BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            if (proxy.isAuth()) {
                credentialsProvider.setCredentials(
                        new AuthScope(proxy.getIp(), Integer.parseInt(proxy.getPort())),
                        new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword().toCharArray()));
            }

HttpHost p = new HttpHost(proxy.getIp(), Integer.parseInt(proxy.getPort()), "http");

asyncClient = HttpAsyncClients.custom()
                    .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1)
                    .setConnectionManager(cm)
                    .setUserAgent(Utils.USER_AGENT)
                    .setDefaultCookieStore(cookieStore)
                    .setDefaultCredentialsProvider(credentialsProvider)
                    .setProxy(p)
                    .build();

asyncClient.start();

然后,我尝试发出无需代理即可工作的发帖请求。带代理的请求也可以使用http而不是https来处理URL。使用https和代理时,我收到以下错误java.lang.IllegalStateException: No tunnel unless connected

0 个答案:

没有答案