簡體   English   中英

帶有https方案的URL中的Apache HttpClient和遠程文件

[英]Apache HttpClient and remote files in URL with https scheme

我正在使用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:我可以通過瀏覽器下載它,因此存在一種獲取該文件的方法。

似乎有兩個問題:

  • 您注冊了該方案以使用3920作為默認端口,這是HTTPS的非標准端口號。 如果服務器實際上在該端口上運行,則必須在瀏覽器中使用以下URL進行訪問: https://www.thedomain.with:3920/my_resource.pdf 由於您在瀏覽器中使用的URL不包含3920端口,因此服務器將在默認端口443上運行,因此您應使用將new Scheme("https", 3920, sf)更改為new Scheme("https", 443, sf)
  • 您的服務器證書中的CN似乎與它的主機名不匹配,這導致SSLPeerUnverifiedException 為了SSLSocketFactory(TrustStrategy, HostnameVerifier)起作用,您將需要使用SSLSocketFactory(TrustStrategy, HostnameVerifier)構造函數,並傳遞不執行此檢查的驗證程序。 Apache為此提供了AllowAllHostnameVerifier

注意:您實際上不應該在生產代碼中使用無操作TrustStrategy和HostnameVerifier,因為從本質上講,這將關閉所有安全性檢查,以驗證遠程服務器,並使您容易受到模擬攻擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM