httpclient实现模拟登陆discuz论坛

    在前面的文章中,我们使用httpclient实现模拟登陆126邮箱,但是不个不厉害,毕竟只是一个网站,现在我们来模拟登陆一种类型的网站,那就是Discuz! ,看着是挺简单的,但是在实现过程还是遇到了一些问题。

     1.首先是”未定义操作“错误

      这个错误是很纠结的,因为我们知道使用httpclient来模拟登陆,要做的就是把网站form表单里面需要的数据全部找到,然后提交,我照着上面把所有的数据准备好了,包含隐藏字段的数据,但是当提交的时候还是出现“未定义操作“的错误,究竟是什么原因呢!~

我就去看源代码了,在论坛的程序里面有那么一句话,叫:

if(!in_array($_GET['action'], array('login', 'logout'))) {
showmessage('undefined_action');
}

     原来就是这个在作怪,但是怎么解决呢,你看都是使用$_GET来获取数据?

我想好想在php里面$_GET也是能获取表单里面的数据的,这算是一个bug吧,于是在表单里面加上:

nameValues.add(new NameValuePair("action", "login"));

    果然问题解决!~

   2.总是放回到登陆界面

    这次就更加奇怪,老师返回登陆界面,弄了半天也不知道是怎么回事?想想可能也是url里面的参数没带过去吧!~

nameValues.add(new NameValuePair("loginsubmit", "yes"));
nameValues.add(new NameValuePair("infloat", "yes"));
nameValues.add(new NameValuePair("lssubmit", "yes"));

  果然,这下子就可以了!~

完整代码:

/**
* create by tingwen.pan
* 博客地址:http://www.cangzhang.com
* 说明:本程序是模拟登陆bbs论坛
*/
package ynu.sanwen.bbs.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class AddCommentsToBbs {

/**
* @param args
*/

private Boolean isProxy;

public AddCommentsToBbs(Boolean isProxy) {
this.isProxy = isProxy;
}

// 设置只是输出错误信息*****************begin*******************
static {
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
"true");
System.setProperty("org.apache.commons.logging"
+ ".simplelog.log.org.apache.commons.httpclient", "error");
}

public void loginBbs(String bbsUrl,String userName, String userPassword) {
HttpClient httpClient = new HttpClient();// 定义一个客户端
System.out.println("-----------------这是模拟登陆论坛的代码---------------------");
// 链接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
10000);
// 读取超时
httpClient.getHttpConnectionManager().getParams().setSoTimeout(18000);

if (isProxy) {
httpClient.getHostConfiguration().setProxy("192.168.1.1", 8080);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
"huanghe", "00000");
httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);
}

PostMethod postMethod = new PostMethod(bbsUrl+"/member.php");// 设置一个post请求
HttpMethodParams params = postMethod.getParams();
params.setContentCharset("UTF-8"); // 设置编码
List nameValues = new ArrayList();// 设置参数列表

nameValues.add(new NameValuePair("mod", "logging"));
nameValues.add(new NameValuePair("action", "login"));
nameValues.add(new NameValuePair("loginsubmit", "yes"));
nameValues.add(new NameValuePair("infloat", "yes"));
nameValues.add(new NameValuePair("lssubmit", "yes"));
nameValues.add(new NameValuePair("fastloginfield", "username"));
nameValues.add(new NameValuePair("username", userName));
nameValues.add(new NameValuePair("password", userPassword));
nameValues.add(new NameValuePair("cookietime", "2592000"));

nameValues.add(new NameValuePair("quickforward", "yes"));
nameValues.add(new NameValuePair("handlekey", "ls"));

List

headers = new ArrayList
();
headers.add(new Header("User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
httpClient.getHostConfiguration().getParams().setParameter(
"http.default-headers", headers);

postMethod.setRequestBody(nameValues
.toArray(new NameValuePair[nameValues.size()]));// 添加参数列表
// 解决cookie报错信息
DefaultHttpParams.getDefaultParams().setParameter(
"http.protocol.cookie-policy",
CookiePolicy.BROWSER_COMPATIBILITY);
try {

httpClient.executeMethod(postMethod);
String returnBody = postMethod.getResponseBodyAsString();

if("".equals(returnBody)){
System.out.println("登陆成功");
}else{
Pattern p1 = Pattern.compile("

(.*)

");
Matcher m1 = p1.matcher(returnBody);
if(m1.find()){
System.out.println("提示信息是:"+m1.group(1));
}
System.out.println("登陆失败");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new AddCommentsToBbs(false).loginBbs("http://localhost/bbs","test123", "test123");
}

}

登陆成功:

登陆失败:

次数过多:

共有 0 条评论

Top