title: RabbitMQ HTTP API request 401 Unauthorized
body: <p>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.</p>
  
  <p>I found this as a solution </p>
  
  <pre><code>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);
  </code></pre>
  
  <p>but it doesn't work. I saw that DefaultHttpClient class is depreciated so I tried something like this </p>
  
  <pre><code>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);
  </code></pre>
  
  <p>and then to access to REST thought WebTarget, something like this</p>
  
  <pre><code>WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");
  </code></pre>
  
  <p>but I still get error 401 Unauthorized. Any suggestion how to solve this problem?</p>