httpclient实现模拟登陆126邮箱

          最近在学习httpclient,顺便就想试试模拟等你一下,我就用126的邮箱作为一个登陆的案列来试试这个技术是不是好用!~其实在实现功能之前我们要理解我们要做的什么,我们需要的是什么,在模拟登陆中,我们都是知道在postmethod中,我们需要传送相关的数据过去,而且我们还必须将数据传到正确的地方,也就是说form表单里面的action需要时正确的。

           实现过程中遇到的问题总结。

           1.form中action的地址,在126的邮箱中,action的地址不是写死的,还是通过js来进行组装的,这样可以带一些参数到服务器端,进行验证,怕的就是别人直接通过表单提交数据。

          2.写入cookie的时候出去,解决方法就是加上一句代码:DefaultHttpParams.getDefaultParams().setParameter(“http.protocol.cookie-policy”, CookiePolicy.BROWSER_COMPATIBILITY);

         3.加上来代理的实现,就是说当你得到网络使用代理的时候,可以直接启用。

        下面直接放上代码吧,看着是挺不错的!~直接通过代码来判断是不是成功登陆。。。

         实现代码:

        

package ynu.sanwen.email.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.Cookie;
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.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class SendMessagesWithEmail {
private Boolean isProxy;

public SendMessagesWithEmail(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");
}
// 设置只是输出错误信息*****************end*******************

public void loginEmail(String userName, String userPassword) {
HttpClient httpClient = new HttpClient();// 定义一个客户端
String goUrl="";

System.out.println("SendMessagesWithEmail.loginEmail()");
// 链接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
10000);
// 读取超时
httpClient.getHttpConnectionManager().getParams().setSoTimeout(18000);

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

PostMethod postMethod = new PostMethod(
"https://ssl.mail.126.com/entry/cgi/ntesdoor?hid=10010102&funcid=loginone&df=webmail126&language=-1&passtype=1&verifycookie=-1&iframe=1&from=web&net=failed&product=mail126&style=-1&race=-2_-2_-2_db&uid=pant_ingwen@126.com");// 设置一个post请求
HttpMethodParams params = postMethod.getParams();
params.setContentCharset("UTF-8"); // 设置编码

List nameValues = new ArrayList();// 设置参数列表

nameValues.add(new NameValuePair("username", userName));
nameValues.add(new NameValuePair("password", userPassword));
nameValues.add(new NameValuePair("savelogin", "yes"));
nameValues.add(new NameValuePair("url2",
"http://mail.126.com/errorpage/err_126.htm"));
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();
String urlPattern="http://cwebmail.mail.126.com/js5/main.jsp?(.*);";
Pattern p1=Pattern.compile(urlPattern);
Matcher m1 = p1.matcher(returnBody);
//System.out.println(returnBody);
if(m1.find()){
goUrl=m1.group().substring(0,(m1.group().length()-2));
System.out.println("登陆成功");
System.out.println("跳转地址是:"+goUrl);

GetMethod getMethod = new GetMethod(goUrl);
System.out.println("执行跳转地址*******************");
httpClient.executeMethod(getMethod);
//System.out.println(getMethod.getResponseBodyAsString());
Cookie[] cookies =httpClient.getState().getCookies();
System.out.println(cookies.length);
System.out.println(cookies[0].getName());
System.out.println(cookies[0].getValue());

System.out.println("处理之中。。。。。。。。。");
}else{
System.out.println("登陆失败");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}

}

  测试代码:

package ynu.sanwen.email.test;

import ynu.sanwen.email.model.SendMessagesWithEmail;

public class TestSend {

/**
* @param args
*/
public static void main(String[] args) {
new SendMessagesWithEmail(false).loginEmail("pant_ingwen@126.com","test");;
}

}

 结果:

显然是失败,我不可能把代码告诉你们吧!~

下面截图就是真的登陆成功,但是你看不到密码了

共有 1 条评论

Top