0

I want to execute the below command through Java code. This is to create connection to my Appdynamics Contoller

curl --user user@customer1:password 'http://192.168.1.11:9090/controller/rest/applications?output=JSON'

The java code that I have written for this is

import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Test {
private static HttpClient client;

public static void main(String[] args) {
    CloseableHttpResponse response = null;
    CloseableHttpClient httpclient = null;
    try {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("192.168.1.11", 9090), new UsernamePasswordCredentials(
                "user", "password"));
        httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

        HttpPost httpget = new HttpPost("http://192.168.1.11:9090/controller/rest/applications?output=JSON");

        System.out.println("Executing request " + httpget.getRequestLine());
        response = httpclient.execute(httpget);
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
            httpclient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

I keep on getting below error

HTTP/1.1 401 Unauthorized Error report

HTTP Status 401 -


type Status report

message

descriptionThis request requires HTTP authentication ().


Can anyone please help.

Rachit M Garg
  • 263
  • 4
  • 19

0 Answers0