java - 如何使用Monit Web Service API java

标签 java rest monit

我正在尝试使用以下代码连接 monit Web 服务 api(Restful 服务):

HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:2812/z_security_check");

NameValuePair[] credentials = {
                new BasicNameValuePair("z_username", "admin"),
                new BasicNameValuePair("z_password", "monit"),
                new BasicNameValuePair("z_csrf_protection", "off")
        };

 List<NameValuePair> body = Arrays.asList(credentials);

 httpPost.setEntity(new UrlEncodedFormEntity(body));
 HttpResponse response = client.execute(httpPost);

 System.out.println("Post parameters : " + httpPost.getEntity().getContent());
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

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

 StringBuffer result = new StringBuffer();
 String line = "";
 while ((line = rd.readLine()) != null) {
            result.append(line);
 }

 System.out.println(result.toString());

但我总是有相同的答案:

Post parameters : java.io.ByteArrayInputStream@79d0569b
Response Code : 401
<html><head><title>401 Unauthorized</title></head><body bgcolor=#FFFFFF><h2>Unauthorized</h2>You are not authorized to access monit. Either you supplied the wrong credentials (e.g. bad password), or your browser doesn't understand how to supply the credentials required<hr><a href='http://mmonit.com/monit/'><font size=-1>monit 5.14</font></a></body></html>

Process finished with exit code 0

但是当我通过浏览器输入时(http://localhost:2812/)询问我凭据并正确显示信息

最佳答案

显然monit的Web服务仅在付费版本M/Monit中可用,我通过使用主页的jsup进行Web Scraping并获取必要的数据来解决它

关于java - 如何使用Monit Web Service API java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711871/

相关文章:

Java 相当于 Ant 的 Java 任务中的 fork?

rest - 使用 Powershell 将文件上传到 Box.com

javascript - 用于匹配错误和相关几行的正则表达式

memcached - 在没有 pidfile 的情况下监控 memcached 配置

couchdb - Ubuntu 14.04 上没有 CouchDB 的 pid 文件

java - token 编码/解码问题 - Spring

java - android - 在带有滚动的可扩展 ListView 中加载json

java - 如何使用 VTD-XML 解析器将嵌套的 XML 文件元素放入对象列表?

java - GET 请求正文中的 RESTful Web 服务和 JSON 文档

mysql - 如何使用一个 REST URL 从两个表获取数据?