我正在尝试使用以下代码连接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 /)进入时,问我凭据并正确显示信息
答案 0 :(得分:1)
显然monit的web服务仅在付费版本M / Monit中可用,我通过使用主页面的jsup进行Web Scraping来解决它并获得必要的数据