HTTPClient实现java自动登录人人网

原创 2013年12月02日 20:11:02
参考网址:
https://passport.csdn.net/account/login 
http://www.iteye.com/topic/638206
httpClient
http://bbs.csdn.net/topics/390495789 
http://developer.51cto.com/art/200806/76048.htm
http://my.oschina.net/lldy/blog/57086
http://blog.csdn.net/yodlove/article/details/5938022 
http://java.chinaitlab.com/net/870192.html
http://blog.csdn.net/wguoyong/article/details/6883706 
http://www.holdjava.com/networkprogramme/213519.htm
http://www.th7.cn/Program/java/2011/08/26/40877.shtml
http://www.oschina.net/question/96568_91032
http://blog.csdn.net/passportandy/article/details/7101913 
http://www.cr173.com/soft/61128.html
http://wenku.baidu.com/link?url=d4RXqVqu05FmVVc23zuL0bA8Q9CXIaOOeBu7lYm9mlEaUwFp3X9EGfxOldUqO9pQtIh6Cf37IclGbTURFPnZRBGkn-tjYI3_vFUO2J5PVn7
http://www.oschina.net/question/1378722_130120
http://csstome.iteye.com/blog/936276
http://extjs2.iteye.com/blog/807039
http://www.docin.com/p-611908008.html
http://blog.163.com/ww20042005@126/blog/static/949490452010101102817765/
http://www.pudn.com/downloads322/sourcecode/java/jsp/detail1422233.html
http://www.oschina.net/question/944872_111722
http://bbs.csdn.net/topics/390651559?page=1#post-396177585 
http://www.52pojie.cn/thread-56913-1-1.html
http://www.yc-edu.org/javapeixun/2129.html
 
Java code
1  
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
//import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
public class Test{
    publicstaticvoidmain(String[] args)throwsClientProtocolException,IOException{
    String loginurl="http://www.renren.com/PLogin.do";
    String username="*****@qq.com";
    String password="*****";
    System.out.println(Test.posturl(loginurl,username,password));
}
publicstaticString posturl(String loginurl,String username,String
     password)throwsClientProtocolException, IOException{
 
     HttpClient httpclient1 =newDefaultHttpClient();
     HttpPost httppost =newHttpPost(loginurl);
     List<NameValuePair> formparams =newArrayList<NameValuePair>();
     formparams.add(newBasicNameValuePair("email",username));
     formparams.add(newBasicNameValuePair("password",password));
     UrlEncodedFormEntity entity =newUrlEncodedFormEntity(formparams,"utf-8");
     httppost.setEntity(entity);
     String str="";
     HttpClient httpclient2=null;
     try{
     HttpResponse response1 = httpclient1.execute(httppost);
     String login_success=response1.getFirstHeader("Location").getValue();//获取登陆成功之后跳转链接
          
     System.out.println(login_success);
    
     HttpGet httpget =newHttpGet(login_success);
     httpclient2 =newDefaultHttpClient();
     HttpResponse response2=httpclient2.execute(httpget);
     str=EntityUtils.toString(response2.getEntity());
      httppost.abort();
      httpget.abort();
     }finally{
   httpclient1.getConnectionManager().shutdown();
   httpclient2.getConnectionManager().shutdown();
  }
 
return str;
}
}

 

 

 

 

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 staticvoid main(String[] args) { 
  73.         RenRen renRen = new RenRen(); 
  74.         renRen.printText(); 
  75.     } 


 

相关文章推荐

Java爬虫系列之四模拟登录【模拟登录人人网】

通过前面的学习,我们已经可以对不需要登录的网页正常访问,但现在的网页大部分都需要用户注册,因此这里以学人人网为例,学习一下网站的模拟登录。         首先对http://www.renren.c...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

httpclient 模拟登录人人网, 发心情(刷屏)

登录的站点是3g.renren.com 因为是手机人人, 页面比较简单 首先用HttpGet取出"http://3g.renren.com"的html代码, 是用Jsoup解析出登录表单, 包括验证...
  • Ping_QC
  • Ping_QC
  • 2012年04月22日 20:22
  • 4666

python应用之自动登录人人网,并访问最近来访者

代码如下: ##-*- coding : gbk -*- #在 import os from xml.dom import minidom import re import urllib i...

HTTPClient模拟登陆人人网

目的: 使用HTTPClient4.0.1登录到人人网,并从特定的网页抓取数据。 总结&注意事项: HttpClient(DefaultHttpClient)代表了一个...

httpclient/jsoup模拟登陆人人网

HttpClient(DefaultHttpClient)代表了一个会话,在同一个会话中,HttpClient对cookie自动进行管理(当然,也可以在程序中进行控制)。在同一个会话中,当使用post...

HttpClient模拟登陆人人网,并且爬取日志内容(一)

使用HttpClient最新版本,下载地址Download 注释已经写的比较清楚了,就不再说明了。 爬取到的html内容,既可以用java正则表达式截取,也可以用jsoup(下载地址:Downlo...
  • iisgirl
  • iisgirl
  • 2012年04月28日 13:28
  • 6188

用HttpClient抓取人人网高校数据库(省,高校,院系三级级联)--更新1

更新备注:将src文件改成了一个完整的项目,解压后可以直接导入到Eclipse中去,省去大家配置(项目乱码请改项目属性为GBK)。另外,如果你要登陆人人网 的话,需要申请一个人人网账号。这里提供公用的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HTTPClient实现java自动登录人人网
举报原因:
原因补充:

(最多只允许输入30个字)