2017-11-01 10:15:40
0
I am trying to connect to a Zabbix server. I am able to access the Zabbix server running on port 10051 via the web browser, however, I am unable to connect to the same server using Java API. Following is the code that I am using to connect
import io.github.cgi.zabbix.api.DefaultZabbixApi;
import io.github.cgi.zabbix.api.ZabbixApi;
public class TestClass {
ZabbixApi zabbixApi;
public static void main(String[] args) {
TestClass tc = new TestClass();
tc.before();
tc.testLogin();
// tc.testVersion();
}
public void before() {
String url = "http://xxx.xxx.xxx.xxx:10051/zabbix/api_jsonrpc.php";
zabbixApi = new DefaultZabbixApi(url);
zabbixApi.init();
}
public void testLogin() {
String user = "admin";
String password = "zabbix";
boolean login = zabbixApi.login(user, password);
System.out.println("login result:" + login);
}
public void testVersion() {
String version = zabbixApi.apiVersion();
System.err.println(version);
System.out.println("version := " + version);
}
}
Classpath is:
Error while running this code is :
Exception in thread "main" java.lang.RuntimeException: DefaultZabbixApi call exception!
at io.github.cgi.zabbix.api.DefaultZabbixApi.call(DefaultZabbixApi.java:142)
at io.github.cgi.zabbix.api.DefaultZabbixApi.login(DefaultZabbixApi.java:72)
at TestClass.testLogin(TestClass.java:25)
at TestClass.main(TestClass.java:12)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:839)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at io.github.cgi.zabbix.api.DefaultZabbixApi.call(DefaultZabbixApi.java:138)
... 3 more
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:149)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:286)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:257)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:207)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:684)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
... 7 more
Need help in connecting to Zabbix server using Java API.