【问题标题】:How to consume Monit Web Service API java如何使用 Monit Web Service API java
【发布时间】:2016-02-29 23:30:42
【问题描述】:

我正在尝试使用以下代码连接 monit web service api (Restful Services):

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/)询问我的凭据并正确显示信息

【问题讨论】:

    标签: java rest monit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多