简体   繁体   English

如何使用Monit Web Service API Java

[英]How to consume Monit Web Service API java

I'm trying to connect with monit web service api (Restful Services), using the following code: 我正在尝试使用以下代码连接monit Web服务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());

But always I have the same answer: 但是我总是有相同的答案:

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

but when I enter via browser (http: // localhost: 2812 /) asked me the credentials and displays the information correctly 但是当我通过浏览器(http:// localhost:2812 /)输入时,我询问了凭据并正确显示了信息

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM