📄 formlogindemo.java~7~
字号:
package org.apache.commons.httpclient.demo;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*;
/**
* 用来演示登录表单的示例
* @author Liudong
*/
public class FormLoginDemo {
static final String LOGON_SITE = "90.0.12.20";
static final int LOGON_PORT = 8088;
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
//模拟登录页面login.jsp->main.jsp
PostMethod post = new PostMethod("/NationWideAdmin/test/FormLoginDemo.jsp");
NameValuePair name = new NameValuePair("name", "ld");
NameValuePair pass = new NameValuePair("password", "ld");
post.setRequestBody(new NameValuePair[] {name, pass});
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
//查看cookie信息
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/NationWideAdmin/test/", false,client.getState().getCookies());
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
//访问所需的页面main2.jsp
GetMethod get = new GetMethod("/NationWideAdmin/test/FormLoginDemo1.jsp");
client.executeMethod(get);
//System.out.println(get.getResponseBodyAsString());
//打印结果页面
String response = new String(get.getResponseBodyAsString());
//打印返回的信息
System.out.println("===================================");
System.out.println("返回的求内容:");
System.out.println(response);
System.out.println("===================================");
get.releaseConnection();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -