【问题标题】:Rabbitmq HTTP API request UnAuthorizedRabbitmq HTTP API 请求未经授权
【发布时间】:2012-05-18 06:15:26
【问题描述】:

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

【问题讨论】:

  • 您检查过防火墙权限吗?

标签: api rest httprequest rabbitmq unauthorized


【解决方案1】:

已经用其他方式解决了问题。使用 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

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多