httpclient实现模拟登陆126邮箱

文章出处:http://www.cangzhang.com/1144.html

文章原作者有点坏,他贴上去的代码只能让你运行失败,为什么呢,他在代码上动了一点点手脚,就在文中的正则表达式上,还好只是这么一点点。可能原作者希望读者自己也认真的思考并锻炼一下吧。下面贴上的代码是我改过的,其实改动的地方只有这一句: String urlPattern = "http://mail.126.com/js6/main.jsp?(.*);";


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 = "";
// 链接超时
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((NameValuePair[]) 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();
//过滤url的正则表达式
String urlPattern = "http://mail.126.com/js6/main.jsp?(.*);";
Pattern p1 = Pattern.compile(urlPattern);
Matcher m1 = p1.matcher(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();
}
}

/**
* @param args
*/
public static void main(String[] args) {
new SendMessagesWithEmail(false).loginEmail("xxx@126.com","xxx");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值