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=d4RXqVqu05FmVVc23zuL0bA8Q9CXIaOOeBu7lYm9mlEaUwFp3X9EGfxOldUqO9pQtIh6Cf37Ic*URFPnZRBGkn-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;

}
}
  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. // Don't change the following URL
  7. privatestatic String renRenLoginURL ="http://www.renren.com/PLogin.do";
  8. // The HttpClient is used in one session
  9. private HttpResponse response;
  10. private DefaultHttpClient httpclient =new DefaultHttpClient();
  11. privateboolean login() {
  12. HttpPost httpost = new HttpPost(renRenLoginURL);
  13. // All the parameters post to the web site
  14. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  15. nvps.add(new BasicNameValuePair("origURL", redirectURL));
  16. nvps.add(new BasicNameValuePair("domain","renren.com"));
  17. nvps.add(new BasicNameValuePair("isplogin","true"));
  18. nvps.add(new BasicNameValuePair("formName",""));
  19. nvps.add(new BasicNameValuePair("method",""));
  20. nvps.add(new BasicNameValuePair("submit","登录"));
  21. nvps.add(new BasicNameValuePair("email", userName));
  22. nvps.add(new BasicNameValuePair("password", password));
  23. try {
  24. httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  25. response = httpclient.execute(httpost);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. returnfalse;
  29. } finally {
  30. httpost.abort();
  31. }
  32. returntrue;
  33. }
  34. private String getRedirectLocation() {
  35. Header locationHeader = response.getFirstHeader("Location");
  36. if (locationHeader == null) {
  37. returnnull;
  38. }
  39. return locationHeader.getValue();
  40. }
  41. private String getText(String redirectLocation) {
  42. HttpGet httpget = new HttpGet(redirectLocation);
  43. // Create a response handler
  44. ResponseHandler<String> responseHandler = new BasicResponseHandler();
  45. String responseBody = "";
  46. try {
  47. responseBody = httpclient.execute(httpget, responseHandler);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. responseBody = null;
  51. } finally {
  52. httpget.abort();
  53. httpclient.getConnectionManager().shutdown();
  54. }
  55. return responseBody;
  56. }
  57. publicvoid printText() {
  58. if (login()) {
  59. String redirectLocation = getRedirectLocation();
  60. if (redirectLocation != null) {
  61. System.out.println(getText(redirectLocation));
  62. }
  63. }
  64. }
  65. publicstaticvoid main(String[] args) {
  66. RenRen renRen = new RenRen();
  67. renRen.printText();
  68. }
  69. }
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();
}
}
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
上一篇:mysql-proxy负载均衡


下一篇:FZU 1397 保送