JAVA模拟浏览器怎么保留住成功登录的对话信息,可以继续访问其二级页面
小的近日,在用java中httpclient模拟网页登陆时,碰上问题:
private static String userName = "×××××";
private static String password = "×××××";
private static String redirectURL = "http://www.×××.com/home";//没有用 使用getFirstHeader()从服务器取的
// Don't change the following URL
private static String renRenLoginURL = "http://www.×××.com/PLogin.do";
// The HttpClient is used in one session
private HttpResponse response;//用途
private DefaultHttpClient httpclient = new DefaultHttpClient();//一直使用的客户端
private boolean login() {
HttpPost httpost = new HttpPost(renRenLoginURL);
// All the parameters post to the web site
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("origURL", redirectURL));
nvps.add(new BasicNameValuePair("domain", "renren.com"));
nvps.add(new BasicNameValuePair("isplogin", "true"));
nvps.add(new BasicNameValuePair("formName", ""));
nvps.add(new BasicNameValuePair("method", ""));
nvps.add(new BasicNameValuePair("submit", "登录"));
nvps.add(new BasicNameValuePair("email", userName));
nvps.add(new BasicNameValuePair("password", password));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);//执行登录请求 返回信息
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
httpost.abort();//不废除会报错
}
return true;
}
这是登录代码,可以成功登录,但是在登录成功的基础上再访问该网页下的二级页面,就又失败了,小的想知道,如何才能保留住成功登陆的会话session信息,使得再请求该网站上的其他页面时为登陆状态,即通过验证的状态。