2016-02-29 49 views
0

我試圖與monit的Web服務API(RESTful服務)連接,使用下面的代碼:如何飲用monit的Web服務API的Java

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 : [email protected] 
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://本地主機:2812 /)問我的憑據,並顯示信息正確

回答

1

顯然的monit的Web服務只在付費版本可用M/Monit,我通過使用主頁的jsup進行網頁抓取並獲得必要的數據來解決此問題