日期:2014-05-17  浏览次数:20700 次

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信息,使得再请求该网站上的其他页面时为登陆状态,即通过验证的状态。

------解决方案--------------------
原来是淫淫网,这个网站与其各子应用之间都是以单点登录方式进行整合的,而且也对这种工具自动登录的做了防范处理,如果要想直接用HttpClient进行跳转的话,要解决很多JS后期加载的参数问题,客观地说复杂度还是比较高的。