2015-09-02 57 views
1

我想用Zabbix API和Jersey客戶端登錄Zabbix。它應該是簡單的,這是一個HTTP POST請求,與捲曲這樣的:Zabbix API用戶用Jersey客戶端登錄

curl -i -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc":"2.0","method":"user.login","id":1,"params":{"user":"my_uname","password":"my_pass"}}' http://company_host/zabbix/api_jsonrpc.php 
HTTP/1.1 200 OK 
Date: Wed, 02 Sep 2015 18:06:51 GMT 
Server: Apache/2.4.7 (Ubuntu) 
X-Powered-By: PHP/5.5.9-1ubuntu4.3 
Content-Length: 68 
Content-Type: application/json 

{"jsonrpc":"2.0","result":"53a3e46343b5d29eb11678be2775df9b","id":1} 

在Java代碼中,我試圖用Jersey客戶端:

JSONObject mainJObj = new JSONObject(); 
    JSONObject paramJObj = new JSONObject(); 

    mainJObj.put("jsonrpc", "2.0"); 
    mainJObj.put("method", "user.login"); 
    mainJObj.put("id", "1"); 
    paramJObj.put("user", "my_uname"); 
    paramJObj.put("password", "my_pass"); 
    mainJObj.put("params", paramJObj); 

    Client client = Client.create(); 
    WebResource webResource = client.resource("http://company_host/zabbix/api_jsonrpc.php"); 
    ClientResponse response = webResource.type("application/json").post(ClientResponse.class, mainJObj); 

    if (response.getStatus() != 200) { 
     throw new RuntimeIOException("Failed : HTTP error code: " + response.getStatus()); 
    } 
    String token = response.getEntity(String.class); 
    System.out.println("Authentication token: " + token); 

響應返回200 OK,但實體總是空的。

然後我切換到HttpClient的,它工作正常:

 DefaultHttpClient httpClient = new DefaultHttpClient();  
     HttpPost postRequest = new HttpPost("http://company_host/zabbix/api_jsonrpc.php"); 
     StringEntity input = new StringEntity(mainJObj.toString()); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     if (response.getStatusLine().getStatusCode() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + response.getStatusLine().getStatusCode()); 
     } 

     BufferedReader br = new BufferedReader(
        new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 

它打印出來,從服務器的響應:

{"jsonrpc":"2.0","result":"53a3e46343b5d29eb11678be2775df9b","id":"1"} 

爲什麼Jersey客戶端無法從服務器的響應?

回答

1

終於得到了它的工作,必須創建一個的Apache HTTP客戶端,並在球衣客戶端插件它作爲一個處理程序,並解析響應爲流:

 HttpClient apacheClient = HttpClientBuilder.create().build(); 
     Client client = new Client(new ApacheHttpClient4Handler(apacheClient, 
       new BasicCookieStore(), 
       true)); 

     WebResource webResource = client.resource("http://comapany_host/zabbix/api_jsonrpc.php"); 
     ClientResponse response = webResource.type("application/json").post(ClientResponse.class, mainJObj); 

     if (response.getStatus() != 200) { 
      throw new RuntimeIOException("Failed : HTTP error code: " + response.getStatus()); 
     } 

     BufferedReader br = new BufferedReader(
       new InputStreamReader((response.getEntityInputStream()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 
+0

這是完全可以接受一個正確的答案,即使這是你(OP)的回答。這樣對未來的訪問者來說,這是更正確的解決方案。 – fvu