gpt4 book ai didi

java - InternalHttpClient.getParams() 与 UnsupportedOperationException

转载 作者:行者123 更新时间:2023-11-30 10:23:54 30 4
gpt4 key购买 nike

我写了一个JAVA爬虫,尝试使用proxy,忽略任何https认证

但它不适用于

java.lang.UnsupportedOperationException (at 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();
}
}

非常感谢任何解决方案。

最佳答案

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

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

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

关于java - InternalHttpClient.getParams() 与 UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842037/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com