我正在尝试使用以下代码连接 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/