static final int LOGON_PORT = 80;
public FormLoginDemo() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
PostMethod authpost = new PostMethod("/servlet/SessionServlet");
// 準備登錄參數
NameValuePair action = new NameValuePair("action", "login");
NameValuePair url = new NameValuePair("url", "/index.html");
NameValuePair userid = new NameValuePair("UserId", "userid");
NameValuePair password = new NameValuePair("Password", "password");
authpost.setRequestBody(
new NameValuePair[] {action, url, userid, password});
// 執行Post請求
client.executeMethod(authpost);
// 打印狀態碼
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// 釋放連接
authpost.releaseConnection();
}
}