rabbitmq http json api访问

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URLEncoder;


import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.AuthCache;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.protocol.ClientContext;

import org.apache.http.impl.auth.BasicScheme;

import org.apache.http.impl.client.BasicAuthCache;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HTTP;


public class HealthCheckController {

private Log log = LogFactory.getLog(HealthCheckController.class);

private static final String SCHEME = "http";

private static final String HOST_NAME = "192.168.1.82";

private static final int PORT = 15672;

private static final String OK = "ok";

private static final String USER = "admin";

private static final String PASSWORD = "admin";

public static void main(String[] args) {

try {

new HealthCheckController().check();

} catch (IOException e) {

e.printStackTrace();

}

}


public void check() throws IOException {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpHost targetHost = new HttpHost(HOST_NAME, PORT, SCHEME);

        //HttpPut rq = new HttpPut("/api/overview");

        //HttpGet rq = new HttpGet("/api/overview");

        //HttpGet rq = new HttpGet("/api/cluster-name");

        //HttpGet rq = new HttpGet("/api/nodes");

        //HttpGet rq = new HttpGet("/api/extensions");

        //HttpGet rq = new HttpGet("/api/connections");

        //HttpGet rq = new HttpGet("/api/exchanges");

        //HttpGet rq = new HttpGet("/api/aliveness-test/%2f");

        //HttpGet rq = new HttpGet("/api/policies");

        //HttpGet rq = new HttpGet("/api/policies/%2f");

        //HttpGet rq = new HttpGet("/api/parameters");

        //HttpGet rq = new HttpGet("/api/queues");

        //HttpGet rq = new HttpGet("/api/queues/%2f");

        HttpGet rq = new HttpGet("/api/bindings");

        //HttpPost rq = new HttpPost("/api/queues/%2f/qu.email/get");

        

        try {

            httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(USER, URLEncoder.encode(PASSWORD, HTTP.UTF_8)));


            AuthCache authCache = new BasicAuthCache();

            BasicScheme basicAuth = new BasicScheme();

            authCache.put(targetHost, basicAuth);

            BasicHttpContext localcontext = new BasicHttpContext();

            localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);


            HttpResponse rp = httpclient.execute(targetHost, rq, localcontext);

            

            log.info("===================");

            log.info(rp);

            BufferedReader rd = new BufferedReader(new InputStreamReader(rp.getEntity().getContent()));

      String line = "";

            while ((line = rd.readLine()) != null) {

            log.info(line);

            } 

            log.info("===================");

        } finally {

            httpclient.getConnectionManager().shutdown();

        }

}

}

需要用到apache的httpclient4.3.5.jar




你可能感兴趣的:(rabbitmq http json api访问)