[ JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe JDK6 API
直接上代码了,代码里面有详细的注释
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class test {
public static void main(String[] args) {
try {
// 先访问首页,[官方详细介绍官方文档里有个bug追查代码才发现的isVirtualWebappRelative - Should \"virtual\" SSI directivepaths be interpreted as relative to th]得到cookie
// cookie信息自动保存在HttpClient中
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod("http://1.lexury2016.sinaapp.com/login");
httpClient.executeMethod(postMethod);
// 携带cookie访问登录网面
postMethod = new PostMethod("http://1.lexury2016.sinaapp.com/login");
// 设置登录的账号与密码
NameValuePair[] nameValuePairs = { new NameValuePair("stucid", "2013150091"),
new NameValuePair("stupassword", "ab1234") };
postMethod.setRequestBody(nameValuePairs);
// 设置请求编码为UTF-8
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// httpclient访问登录网页
httpClient.executeMethod(postMethod);
// 得到响应文本
byte[] bytes = postMethod.getResponseBody();
String html = new String(bytes);
System.out.println(postMethod.getStatusCode());
// 输出为302,也就是说网页发生了重定向
// 得到重定向后的网页
Header redirect = postMethod.getResponseHeader("location");
String url = redirect.getValue();
// 使用get请求,访问登陆后的页面
GetMethod getMethod = new GetMethod(url);
httpClient.executeMethod(getMethod);
// 得到返回文本
bytes = getMethod.getResponseBody();
html = new String(bytes);
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
}
}
}[本文转http://googledave.javaeye.com/blog/438474ssi直译server side include ,服务器端包括关键字: tomcat ssi shtm]