I am trying to make a get request on an API, that has basic authentication, but even though I have the correct username and password, it won't work.
When I'm trying to reach the API with curl (Linux command) it works fine.
curl -u "user:password" -s https://dummy/link/1
Do I need to specify that the response must be enter code here
JSON?
I am using Maven: org.apache.httpcomponents:httpclient:4.5.13 dependency
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user","password");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpResponse response = client.execute(
new HttpGet("https://dummy/link/1");
int statusCode = response.getStatusLine()
.getStatusCode();
if(statusCode != HttpStatus.SC_OK){
System.out.println(statusCode);
}else{
System.out.println(response);
}