使用org.apache.commons.httpclient方便,效率又高,下面是post方式提交登录参数的代码:
public class FormLoginDemo
{
static final String LOGON_SITE = "developer.java.sun.com";
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();
}
}
把表单
提示:。。。
给你一个简单的html页面,你可以执行下看看是不是你想要的效果!
嗯
可以人为的创建一个表单,然后给出需要的参数和值,用javascript来提交这个表单,就是模拟submit按钮的click