2012-05-18 96 views
4

我正尝试使用http api请求创建新的交换。我用来创建Exchange的URL是http://guest:[email protected]:55672/api/exchanges/%2F/myexq1,但它给了我401未授权的错误。我正在使用Chrome休息客户端来执行此请求。可能是什么原因?任何帮助将不胜感激。Rabbitmq HTTP API请求未授权

+0

你检查防火墙的权限? – robthewolf

+0

我的防火墙关闭了。 –

回答

2

以其他方式解决问题。在使用URL http://guest:[email protected]: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 
+0

这不起作用了。 :(有没有新的解决方案?Tnx –