吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6453|回复: 6
收起左侧

[其他转载] JAVA httpclient 校内网登陆

 关闭 [复制链接]
my26929 发表于 2010-6-12 21:14
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class RrLogin {
static final String LOGON_SITE = "http://www.renren.com";

static final int LOGON_PORT = 80;

public Cookie[] login(String name, String pas) {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("http://passport.renren.com/PLogin.do");
NameValuePair username = new NameValuePair("email", name);
NameValuePair password = new NameValuePair("password", pas);
NameValuePair origURL = new NameValuePair("origURL",
"http://home.renren.com/Home.do");
NameValuePair domain = new NameValuePair("domain", "renren.com");
NameValuePair formName = new NameValuePair("formName", "");
NameValuePair method = new NameValuePair("method", "");
post.setRequestBody(new NameValuePair[] { username, password, origURL,
domain, formName, method });
try {
client.executeMethod(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseString = null;
try {
responseString = new String(post.getResponseBodyAsString()
.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();

// 截取转跳地址
if (responseString.lastIndexOf("The URL has moved") > -1) {
responseString.substring(26, responseString.length() - 10);
String newUrl = responseString.substring(27, responseString
.length() - 11);

// 实施转跳
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;)");
get.setRequestHeader("Cookie", cookies.toString());
try {
client.executeMethod(get);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
get.releaseConnection();
return client.getState().getCookies();
}
return null;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zhuzhenhaoa 发表于 2010-6-12 22:30
WHAT'S THAT ?
aiwanqq 发表于 2010-6-12 22:55
 楼主| my26929 发表于 2010-6-14 20:44
what's the function the below code have?
aiwanqq 发表于 2010-6-12 22:55



    JAVA
ak2500 发表于 2010-6-15 15:39
J2EE
korykim 发表于 2010-6-16 15:35
本帖最后由 korykim 于 2010-6-16 15:47 编辑

org.apache.commons这个包要到http://labs.renren.com/apache-mirror/这里下载才行~!包名httpcomponents/是这个
f8949087456 发表于 2010-6-22 08:35
非常感谢了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-3 22:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表