Zabbix API用户使用Jersey客户端登录

时间:2015-09-02 21:10:50

标签: jersey-client zabbix

我正在尝试使用Zabbix API和Jersey客户端登录Zabbix。它应该是直截了当的,它是一个HTTP Post请求,像这样卷曲:

public Quaternion(float p_x, float p_y, float p_z, float p_w) {
    this.x = p_x;
    this.y = p_y;
    this.z = p_z;
    this.w = p_w;
}

在Java代码中,我尝试使用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}

响应返回200 OK但实体始终为空。

然后我切换到httpclient,它工作正常:

    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);

它打印出来自服务器的响应:

        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);
        }

为什么Jersey客户端无法从服务器获得响应?

1 个答案:

答案 0 :(得分:1)

终于搞定了,必须创建一个apache http客户端并将其作为处理程序插入到jersey客户端并将响应解析为流:

        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);
        }