2017-05-17 174 views
0

捲曲--location我有這樣的捲曲在Java中複製與Java

捲曲-i -L 的https:// {用戶名} {}密碼@ url.com/exportsData

響應

HTTP/1.1 302 Found 
Date: Wed, 17 May 2017 11:29:29 GMT 
Content-Length: 0 
Connection: keep-alive 
Location: https://test-cache-ss.s3.amazonaws.com/queries/... 

HTTP/1.1 200 OK 
x-amz-id-2: sjahjsahfjhsa/safksjafksak+skajfksajfhsajhj= 
x-amz-request-id: 33346FA02921212131 
Date: Wed, 17 May 2017 11:29:31 GMT 
Last-Modified: Wed, 17 May 2017 11:07:57 GMT 
x-amz-expiration: expiry-date="Thu, 01 Jun 2017 00:00:00 GMT", rule-id="Delete after 14 days" 
ETag: "7d519e24ae5c1ce3881c59d0670cb8e4-1" 
Accept-Ranges: bytes 
Content-Type: application/octet-stream 
Content-Length: 1137090 
Server: AmazonS3 

{"USER_ID":"265117459845"} 

這是我的代碼返回我401響應:

List<Long> ids = new ArrayList<Long>(); 
try (final CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { 
    final HttpGet httpGet = new HttpGet("https://" + username + ":" 
      + password + "@url.com/exportsData"); 

    try (CloseableHttpResponse response = httpClient.execute(httpGet)) { 

     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
      final ObjectMapper mapper = new ObjectMapper(); 
      mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
      mapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE); 
      ids = Arrays.asList(mapper.readValue(response.getEntity().getContent(), Long[].class)); 
     } 
    } catch (final JsonParseException e) { 
     logger.error("JsonParseException", e); 
    } catch (final JsonMappingException e) { 
     logger.error("JsonMappingException", e); 
    } catch (final UnsupportedOperationException e) { 
     logger.error("UnsupportedOperationException", e); 
    } catch (final IOException e) { 
     logger.error("IOException", e); 
    } 
} catch (final IOException e1) { 
    logger.error("IOException1", e1); 
} 
return ids; 

以這種方式(總401)

List<Long> ids = new ArrayList<Long>(); 
final CredentialsProvider provider = new BasicCredentialsProvider(); 
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"); 
try (final CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build()) { 
    final HttpGet httpGet = new HttpGet("https://url.com/exportsData"); 

    try (CloseableHttpResponse response = httpClient.execute(httpGet)) { 

     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
      final ObjectMapper mapper = new ObjectMapper(); 
      mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
      mapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE); 
      ids = Arrays.asList(mapper.readValue(response.getEntity().getContent(), Long[].class)); 
     } 
    } catch (final JsonParseException e) { 
     logger.error("JsonParseException", e); 
    } catch (final JsonMappingException e) { 
     logger.error("JsonMappingException", e); 
    } catch (final UnsupportedOperationException e) { 
     logger.error("UnsupportedOperationException", e); 
    } catch (final IOException e) { 
     logger.error("IOException", e); 
    } 
} catch (final IOException e1) { 
    logger.error("IOException1", e1); 
} 
return ids; 

還測試增加HttpClientBuilder

.setRedirectStrategy(new LaxRedirectStrategy()).build() 

我已經錯過了還測試?

+0

@DanielStenberg對!我更新了標題 –

+0

@SME_Dev中間沒有代理服務器。我也測試過將setsetedirectstrategy(新的LaxRedirectStrategy())設置到HttpClientBuilder中,但是錯誤總是一樣的... –

回答

0

我一開始並不理解這個問題。如果下面不是問題,我建議殺青日誌級別,看看發生了什麼(https://hc.apache.org/httpcomponents-client-ga/logging.html

- 最初的解決方案 -

你需要傳遞的,而不是將它們添加到憑據到客戶端網址。

查看這裏給出的例子:http://www.baeldung.com/httpclient-4-basic-authentication

+1

看來他寧願遵循302重定向... –

+0

我也按照建議測試過,但狀態碼相同。我在任何情況下更新了問題以顯示我測試過的內容 –