打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
HTTPClient模拟登陆人人网 - rob's blog - JavaEye技术网站
目的:
使用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.     }  
  76. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
httpClient验证码登录
HttpClient4.0基础用法
Android上如何正确实现程序的联网 事关WIFI/CMWAP/CMNET
7.1.4 Android HTTP请求方式:HttpClient 
HttpClient4.0、HttpCore4.0的基础 - 小刚的站 - JavaEye技术网站
HttpClient4.0.1应用指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服