4

我正在尝试使用 http api 请求创建一个新的交换。我用来创建 Exchange 的 URL 是 ,http://guest:guest@localhost:55672/api/exchanges/%2F/myexq1但它给了我 401 Unauthorized 错误。我正在使用 chrome rest 客户端来执行此请求。可能是什么原因?任何帮助将不胜感激。

4

1 回答 1

2

有其他方式解决问题。使用 URL 时出现错误http://guest:guest@localhost:55672/api/exchanges/%2F/myexq1。但为了实现我的目标,我写了一个小班。这是代码:

        DefaultHttpClient httpClient = new DefaultHttpClient();         
        HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 55672, "http");

        HttpPut request = new HttpPut(
                "/api/queues/%2F/q1");

        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);

我包括的罐子是:

commons-codec-1.4
commons-logging-1.1.1
httpclient-4.1.3
httpclient-cache-4.1.3
httpcore-4.1.4
httpmime-4.1.3
于 2012-05-19T06:35:35.843 回答