HTTPClient实现java自动登录人人网
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;
}
}
|
- publicclass RenRen {
- // The configuration items
- privatestatic String userName ="YourMailinRenren";
- privatestatic String password ="YourPassword";
- privatestatic String redirectURL ="http://blog.renren.com/blog/304317577/449470467";
- // Don't change the following URL
- privatestatic String renRenLoginURL ="http://www.renren.com/PLogin.do";
- // The HttpClient is used in one session
- private HttpResponse response;
- private DefaultHttpClient httpclient =new DefaultHttpClient();
- privateboolean 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();
- returnfalse;
- } finally {
- httpost.abort();
- }
- returntrue;
- }
- private String getRedirectLocation() {
- Header locationHeader = response.getFirstHeader("Location");
- if (locationHeader == null) {
- returnnull;
- }
- 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;
- }
- publicvoid printText() {
- if (login()) {
- String redirectLocation = getRedirectLocation();
- if (redirectLocation != null) {
- System.out.println(getText(redirectLocation));
- }
- }
- }
- publicstaticvoid main(String[] args) {
- RenRen renRen = new RenRen();
- renRen.printText();
- }
- }
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文章版权属于作者,受法律保护。没有作者书面许可不得转载。