// 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 java.io.IOException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Login {
    private HttpClient client;
    
    public Login() {
        client = new HttpClient();
    }  
    
    public void login(String url,
                       String userField, String userValue,
                       String passField, String passValue) {
         NameValuePair[] nvp = new NameValuePair[2];
         nvp[0] = new NameValuePair(userField, userValue);
         nvp[1] = new NameValuePair(passField, passValue);
         post(url, nvp);
    }
    public String get(String url) {
        GetMethod met = new GetMethod(url);
        try {
            client.executeMethod(met);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return met.getResponseBodyAsString();
    }
    public String post(String url, NameValuePair[] nvp) {
        PostMethod met = new PostMethod(url);
        if(nvp != null) {
            met.setRequestBody(nvp);
        }
      try {
         client.executeMethod(met);
      } catch (HttpException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
        return met.getResponseBodyAsString();
    }
   public static void main(String[] args) {
        Login lgi = new Login();
        lgi.login("http://arne:8080/useradmin/Login",
                  "username", args[0],
                  "password", args[1]);
        System.out.println(lgi.get("http://arne:8080/useradmin/UserAdmin.jsp"));
   }
}
