0

我想通过 Java 代码执行下面的命令。这是为了创建与我的 Appdynamics 控制器的连接

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

我为此编写的java代码是

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();
        }
    }
}
}

我不断低于错误

HTTP/1.1 401 未经授权的错误报告

HTTP 状态 401 -


类型状态报告

信息

描述该请求需要 HTTP 身份验证 ()。


任何人都可以请帮忙。

4

0 回答 0