// 登陆 Url
String loginUrl = "http://bbs.fuli.ba/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes";
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod(loginUrl);
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
NameValuePair[] param = { new NameValuePair("username","XXX"),
new NameValuePair("password","xxx"),
new NameValuePair("cookietime","2592000"),
new NameValuePair("refferer","http://bbs.fuli.ba/forum-4-1.html") } ;
post.setRequestBody(param);
try {
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
int i = httpClient.executeMethod(post);
System.out.println("************************"+i);
// 获得登陆后的 Cookie
Cookie[] cookies = httpClient.getState().getCookies();
StringBuffer tmpcookies = new StringBuffer();
for (Cookie c : cookies) {
tmpcookies.append(c.toString() + ";");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(),"utf-8"));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while((str = reader.readLine())!=null){
stringBuffer.append(str);
}
String ts = stringBuffer.toString();
//System.out.println(ts);
String[] success = ts.split("欢迎您回来");
if(success.length>0){
System.out.println("*************登陆成功"+success[1].substring(0,15));
}
//从登陆成功后的数据去判断签到的连接
String[] st = ts.split("id=fx_checkin:checkin&formhash=");
String fromhash = st[1].substring(0, 8)+"&"+st[1].substring(0, 8);
// String qiandaourl ="http://bbs.fuli.ba/plugin.php?id=fx_checkin:checkin&formhash="+fromhash;
//System.out.println(qiandaourl);
String qiandaourl = "http://bbs.fuli.ba/home.php?mod=space&uid=51923";
// 进行登陆后的操作
GetMethod getMethod = new GetMethod(qiandaourl);
// 每次访问需授权的网址时需带上前面的 cookie 作为通行证
getMethod.setRequestHeader("Cookie", tmpcookies.toString());
System.out.println(tmpcookies.toString());
// 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
// 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
getMethod.setRequestHeader("Referer", "http://bbs.fuli.ba/forum-4-1.html");
getMethod.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
getMethod.setRequestHeader("Host","bbs.fuli.ba");
getMethod.setRequestHeader("Connection","keep-alive");
getMethod.setRequestHeader("Accept-Language","zh-CN,zh;q=0.8");
getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36");
int j = httpClient.executeMethod(getMethod);
System.out.println("******************"+j);
// 打印出返回数据,检验一下是否成功
String returnBody2 = getMethod.getResponseBodyAsString();
String newStr = new String(returnBody2.getBytes(), "gbk");
System.out.println(newStr);
post.releaseConnection();//这里最好把之前的资源放掉
getMethod.releaseConnection();