用Java获取Appdynamics数据

时间:2016-04-13 11:15:37

标签: java http appdynamics

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

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 -


类型状态报告

消息 < / p>

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


任何人都可以帮忙。

0 个答案:

没有答案