【问题标题】:javax.net.ssl.HttpsURLConnection vs org.apache.http.client.HttpClientjavax.net.ssl.HttpsURLConnection 与 org.apache.http.client.HttpClient
【发布时间】:2021-04-05 07:40:43
【问题描述】:

我正在尝试从 apache httpClient 获取响应信息,但我没有得到我需要的信息。 HttpsURLConnection 过去给了我一些问题,我不想使用它。我正在尝试从对这些库有深入了解并为我提供原因或解决方案的人那里获得帮助。

当我尝试使用 HttpsURLConnection 调用 URL 时,它为我提供了所有“文档”url 调用,我可以遍历所有...

public InputStream getResource(String resource, String username, String password) throws Exception {
        int redirects = 0;

        // Place an upper limit on the number of redirects we will follow
        while (redirects < 10) {
            ++redirects;

            // Configure a connection to the resource server and submit the request for our resource.
            URL url = new URL(resource);
            HttpsURLConnection connection = null;
            if (url.getProtocol().equalsIgnoreCase("https")) {
                connection = (HttpsURLConnection) url.openConnection();
            } else {
                connection = (HttpsURLConnection) new URL("https", url.getAuthority(), url.getFile()).openConnection();
            }
            connection.setRequestMethod("GET");
            connection.setInstanceFollowRedirects(false);
            connection.setUseCaches(false);
            connection.setDoInput(true);

            // If this is the URS server, add in the authentication header.
            if (resource.startsWith(URS)) {
                connection.setDoOutput(true);
                connection.setRequestProperty("Authorization",
                        "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
            }

            if (connection.getResponseCode() == 200) {
                return connection.getInputStream();
            }

            if (connection.getResponseCode() != 302) {
                throw new Exception("Invalid response from server - status " + connection.getResponseCode());
            }
            
            resource = connection.getHeaderField("Location");
        }
        
        throw new Exception("Redirection limit exceeded");
    }

... 使用此代码,我可以遍历每个 url,我可以发送基本身份验证,然后登录页面。在它之后,我然后打电话给其他时间并获取它的信息......

使用 HttpsURLConnection 我得到了这 2 个文档,我刚刚阅读了第一个文档的“位置”。然后我记得使用基本身份验证。

如果我尝试对 org.apache.http.client.HttpClient 做同样的事情......

public Optional<HttpPayloadResponse> getResource(String resource, String username, String password) throws Exception {
        int redirects = 0;

        // Place an upper limit on the number of redirects we will follow
        while (redirects < 10) {
            ++redirects;
            
            BasicHttpQuery basicHttpQuery = new BasicHttpQuery();
            basicHttpQuery.setTimeOutMillis(20000);
            basicHttpQuery.setHttpRequestType(HttpRequestTypeEnum.GET);
            basicHttpQuery.setUrl(resource);
            basicHttpQuery.setTimeOutMillis(30000);
            
            Optional<HttpPayloadResponse> response = EoHttpClient.executeHttpQuery(basicHttpQuery);
            
            if (response.get().getResponseHeaders().get("Location").equals(URL_FILE_2_DOWNLOAD)) {
                return response;
            } else {
                // If this is the URS server, add in the authentication header.
                if (resource.startsWith(URS)) {
                    basicHttpQuery.getHttpHeader().put("Authorization", 
                            "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
                }
                
                resource = response.get().getResponseHeaders().get("Location");
            }
        }
        
        throw new Exception("Redirection limit exceeded");
    }

try(CloseableHttpClient httpClient = buildHttpClientNotCheckSsl(archiveQuery.getTimeOutMillis())) {
        
        HttpRequestBase httpOperation = null;
        if (archiveQuery.getHttpRequestType().equals(HttpRequestTypeEnum.GET)) {
            httpOperation = new HttpGet(archiveQuery.getUrl());
        } else {
            HttpPost httpPost = new HttpPost(archiveQuery.getUrl());
            httpPost.setEntity( new StringEntity(archiveQuery.getQueryPayload()));
            httpOperation = httpPost;
        }
        
        for (Map.Entry<String, String> currentHeader : archiveQuery.getHttpHeader().entrySet()) {
            httpOperation.setHeader(currentHeader.getKey(), currentHeader.getValue());
        }
        
        try (CloseableHttpResponse response = httpClient.execute(httpOperation)) {

public static CloseableHttpClient buildHttpClientNotCheckSsl(int timeoutMillis) throws Exception {
     
    RequestConfig requestConfig = RequestConfig.custom().
            setConnectTimeout(timeoutMillis).setConnectionRequestTimeout(timeoutMillis).setSocketTimeout(timeoutMillis).
            build();

    final SSLContext sslContext = new SSLContextBuilder()
            .loadTrustMaterial(null, (x509CertChain, authType) -> true)
            .build();

    return HttpClientBuilder.create()
            .setSSLContext(sslContext)
            .setConnectionManager(
                    new PoolingHttpClientConnectionManager(
                            RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.INSTANCE)
                            .register("https", new SSLConnectionSocketFactory(sslContext,NoopHostnameVerifier.INSTANCE))
                            .build()
                            ))
            .setDefaultRequestConfig(requestConfig)
            .build();
}

它只给了我第二个调用,它没有显示我在导航器的网络开发资源管理器中看到的所有“文档”调用。为什么?有没有办法处理 apache httpClient?

【问题讨论】:

    标签: java http httpclient httpsurlconnection


    【解决方案1】:

    我找到了解决方案。需要在通话中禁用重定向...

    public static void main(String[] args) throws IOException {
    
        HttpGet request = new HttpGet("https://cddis.nasa.gov/archive/products/iers/finals2000A.data");
    
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build();
             CloseableHttpResponse response = httpClient.execute(request)) {
    
            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 301
            System.out.println(response.getStatusLine().getReasonPhrase()); // Moved Permanently
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 301 Moved Permanently
    
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2023-02-11
      • 1970-01-01
      • 2014-06-24
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多