【发布时间】: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