curl - 使用Java定位

时间:2017-05-17 11:40:32

标签: java curl httpclient

我有这个卷曲在java中复制

  

curl -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()

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我一开始并不理解这个问题。如果下面不是问题我建议提高日志级别,看看发生了什么(https://hc.apache.org/httpcomponents-client-ga/logging.html

- 初步解决方案 -

您需要将凭据传递给客户端,而不是将其添加到网址。

查看此处给出的示例:http://www.baeldung.com/httpclient-4-basic-authentication