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 Unauthorized エラー レポート
HTTP ステータス 401 -
タイプステータス レポート
メッセージ
説明このリクエストには HTTP 認証 () が必要です。
誰でも助けてください。