Added ConnectTLS example

This commit is contained in:
Gideon le Grange 2016-01-01 10:03:04 +02:00
parent 0b80064e46
commit 7390be0aa1
1 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright 2015 gideon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import me.legrange.mikrotik.ApiConnection;
import me.legrange.mikrotik.MikrotikApiException;
/**
* Example: Open a TLS connection
*
* @author gideon
*/
public class ConnectTLS {
public static void main(String... args) throws Exception {
ConnectTLS ex = new ConnectTLS();
ex.connect();
ex.test();
ex.disconnect();
}
private void test() throws MikrotikApiException {
con.execute("/system/reboot");
}
protected void connect() throws Exception {
SocketFactory fact = SSLSocketFactory.getDefault();
con = ApiConnection.connect(fact, Config.HOST, ApiConnection.DEFAULT_TLS_PORT, 2000);
con.login(Config.USERNAME, Config.PASSWORD);
}
protected void disconnect() throws Exception {
con.disconnect();
}
private ApiConnection con;
}