【背景】后台需请求一个digest auth认证的接口

【依赖】pom.xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
【代码】代码展示
String URLPRE = "***";
String USERNAME = "***";
String PASSWARD = "***";
private final CredentialsProvider credsProvider = new BasicCredentialsProvider();
private CloseableHttpClient httpClient = null;
private void init(){
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(USERNAME, PASSWORD));
httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
}
public ArrayList<String> getData() throws Exception{
ArrayList<String> repoResult = new ArrayList<>();
String link = URLPRE+"***";
init();
try{
HttpGet request = new HttpGet(link);
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode!=200){
throw new RuntimeException("请求数据状态码为:"+statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(result);
jsonNode.fields().forEachRemaining(entry->{
repoResult.add(entry.getKey());
});
logger.info("list:{}",repoResult);
}
}
} catch (IOException e) {
throw new RuntimeException("请求数据异常");
}
return repoResult;
}