Using Jakarta Commons HttpClient here is a Java snippet that will login to M/Monit and print status data to stdout.

Note that the HttpClient library is doing cookie housekeeping, by tracking and automatically resubmitting the zsessionid cookie for you.

Create a new client instance:

HttpClient client = new HttpClient();

Access the dashboard to get the zsessionid cookie and login form:

GetMethod get = new 
    GetMethod("http://localhost:8080/index.csp");
client.executeMethod(get);
get.releaseConnection();

Authenticate, programmatically post the login form back to M/Monit. We also add z_csrf_protection=off as a parameter to turn CSRF-protection off for this session. Otherwise we would have to obtain and send a CSRF-token with every POST method when accessing /admin/* resources.

PostMethod auth = new 
    PostMethod("http://localhost:8080/z_security_check");
NameValuePair[] credentials = {
    new NameValuePair("z_username", "admin"),
    new NameValuePair("z_password", "swordfish")
    new NameValuePair("z_csrf_protection", "off")
};
auth.setRequestBody(credentials);
client.executeMethod(auth);
auth.releaseConnection();

We are logged in and can start requesting data:

PostMethod status = new 
    PostMethod("http://localhost:8080/status/hosts/list");
client.executeMethod(status);
System.out.println(status.getResponseBodyAsString());
status.releaseConnection();

Logout from M/Monit and release the login session:

GetMethod logout = new 
    GetMethod("http://localhost:8080/login/logout.csp");
logout.setFollowRedirects(false);
client.executeMethod(logout);
logout.releaseConnection();