您的位置:首页 > 理论基础 > 计算机网络

HTTPClient实现java自动登录人人网

2013-12-02 20:11 369 查看
参考网址:
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
Java代码







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 staticvoid 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文章版权属于作者,受法律保护。没有作者书面许可不得转载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: