【问题标题】:InternalHttpClient.getParams() with UnsupportedOperationException带有 UnsupportedOperationException 的 InternalHttpClient.getParams()
【发布时间】:2017-10-20 03:35:21
【问题描述】:

我写了一个JAVA爬虫并尝试使用proxy并忽略任何https certification

但它不适用于

java.lang.UnsupportedOperationException (在 org.apache.http.impl.client.InternalHttpClient.getParams)

我搜索的解决方案大多说我的HttpClient的版本是旧的,但是我从apache网站更新到最新版本,这个异常仍然发生。

以下代码是我的爬虫代码:

public static void main(String[] args) {
    try{
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                }
        };
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, trustAllCerts, null);
        LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(ctx);
        CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();            
        HttpHost proxy = new HttpHost("127.0.0.1", 8888,"http");
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        HttpGet request = new HttpGet("https://www.javaworld.com.tw/jute/post/view?bid=29&id=312144");
        CloseableHttpResponse response = client.execute(request);
        String entity = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println(entity);
    }catch (Exception e) {
        e.printStackTrace();
    }
}

非常感谢任何解决方案。

【问题讨论】:

    标签: java exception httpclient


    【解决方案1】:

    我遇到了同样的问题,这是因为该方法已被弃用并且在最新版本中不可用。

    我尝试了下面的代码,它对我有用

            public static HttpClient createClient() {
            try {
                SSLContextBuilder builder = new SSLContextBuilder();
                builder.useProtocol("TLSv1.2");
                builder.loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                });
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        builder.build());
    
                HttpClientBuilder hcBuilder = HttpClients.custom();
                HttpHost httpProxy = new HttpHost(bundle.getString("PROXY_HOST"), Integer.parseInt(bundle.getString("PROXY_PORT")));
                DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(httpProxy);
                hcBuilder.setRoutePlanner(routePlanner);
    
                CloseableHttpClient httpclient = hcBuilder
                        .setSSLSocketFactory(sslsf).build();
    
                return httpclient;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    

    【讨论】:

    • 感谢您的解决方案。请问,new SSLConnectionSocketFactory(builder.build())的'builder'和bundle.getString("PROXY_HOST")的'bundle'是什么意思?
    • 我已经更新了代码以包含完整的方法。这应该会有所帮助。
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    相关资源
    最近更新 更多