httpclient 认证方式访问http api/resutful api并获取json结果
2016-10-14 10:22
477 查看
最近,因公司线上环境rabbitmq经常发生堆积严重的现象,于是跟运维组讨论,帮助开发个集中监控所有rabbitmq服务器运行情况的应用,需要通过java访问rabbitmq暴露的http api并接收json结果,并将下划线转换为驼峰式,其中包括认证。
httpclient 认证方式访问http api/resutful api并获取json结果的相关代码如下:
httpclient 认证方式访问http api/resutful api并获取json结果的相关代码如下:
package com.medsoft.client.test.action; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) //在jackson 2.x中,设置忽略无法映射的属性通过设置在bean上的注解来实现,在jackson 1.x中,则是通过DeserializationConfig.Feature来设置,2.x已经没有这个接口了 public class Queue { private String name; private String node; private long messages; @JsonProperty("messages_ready") private long messagesReady; @JsonProperty("messages_unacknowledged") private long messagesUnacknowledged; @JsonProperty("message_bytes_ram") private long messageBytesRam; @JsonProperty("message_bytes") private long messageBytes; private long memory; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNode() { return node; } public void setNode(String node) { this.node = node; } public long getMessageBytesRam() { return messageBytesRam; } public void setMessageBytesRam(long messageBytesRam) { this.messageBytesRam = messageBytesRam; } public long getMemory() { return memory; } public void setMemory(long memory) { this.memory = memory; } public long getMessages() { return messages; } public void setMessages(long messages) { this.messages = messages; } }
public static void main(String[] args) { httpGet("http://localhost:15672/api/queues/%2f/" + "test-api"); } public static void httpGet(String url){ //设置认证信息 HttpHost targetHost = new HttpHost("localhost", 15672, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("guest", "guest")); AuthCache authCache = new BasicAuthCache(); authCache.put(targetHost, new BasicScheme()); // Add AuthCache to the execution context final HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); try { HttpClient client = HttpClientBuilder.create().build(); HttpResponse response = client.execute(new HttpGet(url), context); //请求成功 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //取得返回的字符串 String strResult = EntityUtils.toString(response.getEntity()); System.out.println(strResult); //Queue queue = JsonUtils.json2Object(strResult, Queue.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(MapperFeature.USE_ANNOTATIONS, true); --默认情况下,注解不会启用,需要显示启用 objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector()); --默认情况下没有注解解析器,需要设置 Queue queue = objectMapper.readValue(strResult, Queue.class); System.out.println(JsonUtils.toJson(queue)); } else { System.out.print(response.toString()); } } catch (ParseException | IOException e1) { e1.printStackTrace(); } }
相关文章推荐
- restful api 的HTTP认证访问方式
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- 通过HTTP方式获取URL返回结果数据(Java)
- 使用Httpclient调用url方式访问spring mvc中的controller并传json值与返回
- [bigdata-002]python3 +flask 以post方式获取json的api
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
- HttpURLConnection和HttpClient分别以GET和POST获取Json数据
- 【C#MVC】 使用HttpWebRequest访问外部API,并返回json信息 (Access_Key)
- POCO库下访问http获取json串
- 使用HttpURLConnection调用url方式访问spring mvc中的controller并传json值与返回
- (Apache)使用HttpClient方式访问HTTP
- 微博开发原理简化android使用http访问php获取json
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- HttpHost远程访问获取参数方式
- Android 如何用HttpClient 以Get方式获取数据并添加http头信息
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- action访问servlet的API并且获取到MAP或者httpServlet类型的application,session,request
- HTTP访问的两种方式(HttpClient和HttpURLConnection)
- 如何调用http接口获取json数据及GET/POST方式调用http接口