HTTPClient实现java自动登录人人网

参考网址:
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{
 
    public static void main(String[] args) throws ClientProtocolException,IOException{
    String loginurl= "http://www.renren.com/PLogin.do" ;
    String username= "*****@qq.com" ;
    String password= "*****" ;
    System.out.println(Test.posturl(loginurl,username,password));
}
 
 
public static String posturl(String loginurl,String username,String
     password) throws ClientProtocolException, IOException{
 
     HttpClient httpclient1 = new DefaultHttpClient();
     HttpPost httppost = new HttpPost(loginurl);
     List<NameValuePair> formparams = new ArrayList<NameValuePair>();
     formparams.add( new BasicNameValuePair( "email" ,username));
     formparams.add( new BasicNameValuePair( "password" ,password));
     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(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 = new HttpGet(login_success);
     httpclient2 = new DefaultHttpClient();
     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. publicclass RenRen { 
  2.     // The configuration items 
  3.     privatestatic String userName ="YourMailinRenren"
  4.     privatestatic String password ="YourPassword"
  5.     privatestatic String redirectURL ="http://blog.renren.com/blog/304317577/449470467"
  6.  
  7.     // Don't change the following URL 
  8.     privatestatic 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.     privateboolean 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.             returnfalse
  32.         } finally
  33.             httpost.abort(); 
  34.         } 
  35.         returntrue
  36.     } 
  37.  
  38.     private String getRedirectLocation() { 
  39.         Header locationHeader = response.getFirstHeader("Location"); 
  40.         if (locationHeader == null) { 
  41.             returnnull
  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.     publicvoid printText() { 
  64.         if (login()) { 
  65.             String redirectLocation = getRedirectLocation(); 
  66.             if (redirectLocation != null) { 
  67.                 System.out.println(getText(redirectLocation)); 
  68.             } 
  69.         } 
  70.     } 
  71.  
  72.     publicstaticvoid main(String[] args) { 
  73.         RenRen renRen = new RenRen(); 
  74.         renRen.printText(); 
  75.     } 
public class RenRen {
    // The configuration items
    private static String userName = "YourMailinRenren";
    private static String password = "YourPassword";
    private static String redirectURL = "http://blog.renren.com/blog/304317577/449470467";

    // Don't change the following URL
    private static String renRenLoginURL = "http://www.renren.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;
    }

    private String getRedirectLocation() {
        Header locationHeader = response.getFirstHeader("Location");
        if (locationHeader == null) {
            return null;
        }
        return locationHeader.getValue();
    }

    private String getText(String redirectLocation) {
        HttpGet httpget = new HttpGet(redirectLocation);
        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = "";
        try {
            responseBody = httpclient.execute(httpget, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
            responseBody = null;
        } finally {
            httpget.abort();
            httpclient.getConnectionManager().shutdown();
        }
        return responseBody;
    }

    public void printText() {
        if (login()) {
            String redirectLocation = getRedirectLocation();
            if (redirectLocation != null) {
                System.out.println(getText(redirectLocation));
            }
        }
    }

    public static void main(String[] args) {
        RenRen renRen = new RenRen();
        renRen.printText();
    }
}


 

 

posted on 2013-12-03 10:55  我的小人生  阅读(489)  评论(0编辑  收藏  举报