`
gstarwd
  • 浏览: 1489101 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HTTPClient模拟登陆人人网

阅读更多

zz:

目的: http://www.iteye.com/topic/638206
使用HTTPClient4.0.1登录到人人网,并从特定的网页抓取数 据。


总结&注意 事项:

  • HttpClient(DefaultHttpClient)代表了一个会话,在同一个会话中,HttpClient对cookie自动进行管理(当然, 也可以在程序中进行控制)。
  • 在同一个会话中,当使用post或是get发起一个新的请求时,一般需要对调用前一个会话的abort()方法,否则会抛出异常。
  • 有些网站登录成功后会重定向(302, 303),比如这里的人人网。如果发出的是post请求,需要从响应头中取出location,并再次向网站发送请求,以获取最终数据。
  • 抓取程序不要运行地过于频繁,大部分站点都有抵制刷网站机制。人人网访问过于频繁会锁账号。
  • 使用录制工具录制出登录时向网站发出的请求参数。在这里,我使用了badboy,导出成jmeter文件,在jmeter中就可以看到登录时向网站发送的 参数列表和相应的值。
  • 人人网属于登陆流程比较简单的网站,后一篇会介绍一家比较难搞的网站。

代码:
Java 代码
  1. public   class  RenRen {  
  2.     // The configuration items   
  3.     private   static  String userName =  "YourMailinRenren" ;  
  4.     private   static  String password =  "YourPassword" ;  
  5.     private   static  String redirectURL =  "http://blog.renren.com/blog/304317577/449470467 " ;  
  6.   
  7.     // Don't change the following URL   
  8.     private   static  String renRenLoginURL =  "http://www.renren.com/PLogin.do " ;  
  9.   
  10.     // The HttpClient is used in one session   
  11.     private  HttpResponse response;  
  12.     private  DefaultHttpClient httpclient =  new  DefaultHttpClient();  
  13.   
  14.     private   boolean  login() {  
  15.         HttpPost httpost = new  HttpPost(renRenLoginURL);  
  16.         // All the parameters post to the web site   
  17.         List<NameValuePair> nvps = new  ArrayList<NameValuePair>();  
  18.         nvps.add(new  BasicNameValuePair( "origURL" , redirectURL));  
  19.         nvps.add(new  BasicNameValuePair( "domain" "renren.com" ));  
  20.         nvps.add(new  BasicNameValuePair( "isplogin" "true" ));  
  21.         nvps.add(new  BasicNameValuePair( "formName" "" ));  
  22.         nvps.add(new  BasicNameValuePair( "method" "" ));  
  23.         nvps.add(new  BasicNameValuePair( "submit" "登录" ));  
  24.         nvps.add(new  BasicNameValuePair( "email" , userName));  
  25.         nvps.add(new  BasicNameValuePair( "password" , password));  
  26.         try  {  
  27.             httpost.setEntity(new  UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
  28.             response = httpclient.execute(httpost);  
  29.         } catch  (Exception e) {  
  30.             e.printStackTrace();  
  31.             return   false ;  
  32.         } finally  {  
  33.             httpost.abort();  
  34.         }  
  35.         return   true ;  
  36.     }  
  37.   
  38.     private  String getRedirectLocation() {  
  39.         Header locationHeader = response.getFirstHeader("Location" );  
  40.         if  (locationHeader ==  null ) {  
  41.             return   null ;  
  42.         }  
  43.         return  locationHeader.getValue();  
  44.     }  
  45.   
  46.     private  String getText(String redirectLocation) {  
  47.         HttpGet httpget = new  HttpGet(redirectLocation);  
  48.         // Create a response handler   
  49.         ResponseHandler<String> responseHandler = new  BasicResponseHandler();  
  50.         String responseBody = "" ;  
  51.         try  {  
  52.             responseBody = httpclient.execute(httpget, responseHandler);  
  53.         } catch  (Exception e) {  
  54.             e.printStackTrace();  
  55.             responseBody = null ;  
  56.         } finally  {  
  57.             httpget.abort();  
  58.             httpclient.getConnectionManager().shutdown();  
  59.         }  
  60.         return  responseBody;  
  61.     }  
  62.   
  63.     public   void  printText() {  
  64.         if  (login()) {  
  65.             String redirectLocation = getRedirectLocation();  
  66.             if  (redirectLocation !=  null ) {  
  67.                 System.out.println(getText(redirectLocation));  
  68.             }  
  69.         }  
  70.     }  
  71.   
  72.     public   static   void  main(String[] args) {  
  73.         RenRen renRen = new  RenRen();  
  74.         renRen.printText();  
  75.     }  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics