我使用的是 4.2.5 版。来自 org.apache.httpcomponents的AutoRetryHttpClient从方案为https的 url 下载 pdf 文件。代码用 NetBeans 7.3 编写并使用 JDK7。
假设虚构的 pdf 资源位于https://www.thedomain.with/my_resource.pdf
,那么我有以下代码:
SchemeRegistry registry = new SchemeRegistry();
try {
final SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
});
registry.register(new Scheme("https", 3920, sf));
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException ex) {
Logger.getLogger(HttpConnection.class.getName()).log(Level.SEVERE, null, ex);
}
//Here I create the client.
HttpClient client = new AutoRetryHttpClient(new DefaultHttpClient(new PoolingClientConnectionManager(registry)),
new DefaultServiceUnavailableRetryStrategy(5, //num of max retries
100//retry interval));
HttpResponse httpResponse = null;
try {
HttpGet httpget = new HttpGet("https://www.thedomain.with/my_resource.pdf");
//I set header and Mozilla User-Agent
httpResponse = client.execute(httpget);
} catch (IOException ex) {
}
... //other lines of code to get and save the file, not really important since the code is never reached
当我调用client.execute
以下异常时抛出
org.apache.http.conn.HttpHostConnectException: Connection to https://www.thedomain.with refused
我该怎么做才能获得该 pdf 资源?
PS:我可以通过浏览器下载它,所以存在一种获取该文件的方法。