首页 文章

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

提问于
浏览
1

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

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 回答

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

相关问题