// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class FormLogin {
    public static void main(String[] args) throws Exception {
        WebConversation web = new WebConversation();
        // request protected page - will actually get login form
        WebResponse resp = web.getResponse(new GetMethodWebRequest("http://localhost:8080/secure/"));
        System.out.println(resp.getResponseCode() + " " + resp.getContentLength());
        // perform login - will actually get protected page
        WebRequest login = new PostMethodWebRequest("http://localhost:8080/secure/j_security_check");
        login.setParameter("j_username", "tomcat");
        login.setParameter("j_password", "tomcat");
        resp = web.getResponse(login);
        System.out.println(resp.getResponseCode() + " " + resp.getContentLength());
        // request protected page 10 times again
        for(int i = 0; i < 10; i++) {
            resp = web.getResponse(new GetMethodWebRequest("http://localhost:8080/secure/"));
            System.out.println(resp.getResponseCode() + " " + resp.getContentLength());
        }
    }
}
