I'm trying to access to RabbitMQ rest, but I got 401 unauthorized error. I want to access to queue information and to get messages number.
I found this as a solution
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut(
"/api/queues/%2F/queue-name");
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest", "guest"));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
request.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(
"{\"vhost\":\"/\",\"durable\":\"false\",\"auto_delete\":\"false\",\"arguments\":{}}");
request.setEntity(input);
HttpResponse response = httpClient.execute(targetHost, request, localcontext);
but it doesn't work. I saw that DefaultHttpClient class is depreciated so I tried something like this
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut("/api/whoami");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest","guest")
);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialProvider);
context.setAuthCache(authCache);
request.addHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(targetHost, request, context);
and then to access to REST thought WebTarget, something like this
WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");
but I still get error 401 Unauthorized. Any suggestion how to solve this problem?
--
The content of this e-mail, together with any of its attachments, is for the exclusive and confidential use of the named addressee(s) and it may contain legally privileged and confidential information and/or copyrighted material. Any other distribution, use or reproduction without the sender's prior consent is unauthorized and strictly prohibited. If you have by coincidence, mistake or without specific authorization received this e-mail in error, please notify the sender by e-mail immediately, uphold strict confidentiality and neither read, copy, transfer, disseminate, disclose nor otherwise make use of its content in any way and delete the material from your computer.
The content of the e-mail and its attachments is the liability of the individual sender, if it does not relate to the affairs of Betware.
Betware does not assume any civil or criminal liability should the e-mail or it´s attachments be virus infected.