String retCode_0 = "0";// 登录成功
String retCode_5 = "1008";// 用户名不存在
String retCode_20 = "1014";// 密码错误
account = StringUtils.substring(account, 0, account.indexOf("@"));
StringBuilder loginUrl = new StringBuilder("http://fmail.21cn.com/freeinterface/maillogin.jsp?").append("jsoncallback=jsonp")
.append(System.currentTimeMillis()).append("&loginName=").append(account).append("&UserName=").append(account).append("&passwd=")
.append(password);
System.out.println(loginUrl.toString());
String referer = "http://mail.21cn.com/";
String responseStr = crawlHttpClientHelper.doGet(loginUrl.toString(), "", referer);
System.out.println(responseStr);
if (responseStr.indexOf(retCode_0) != -1) {
GetMethod method = new GetMethod("http://hermes.webmail.21cn.com/webmail/login.perform?key=1");
method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml,application/json,text/javascript,*/*;");
method.setRequestHeader("Accept-Language", "zh-cn");
method.setRequestHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; QQDownload 1.7; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; CIBA)");
method.setRequestHeader("Keep-Alive", "300");
method.setRequestHeader("Connection", "Keep-Alive");
method.setRequestHeader("Cache-Control", "no-cache");
method.setRequestHeader("Referer", referer);
method.setRequestHeader("Connection", "close");
// 使用系统提供的默认的恢复策略
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
method.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
method.getParams().setParameter("http.protocol.single-cookie-header", true);
crawlHttpClientHelper.getHttpClient().getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false);
// crawlHttpClientHelper.getHttpClient().getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS,
// 5);
method.setFollowRedirects(false);
int statusCode = crawlHttpClientHelper.getHttpClient().executeMethod(method);
System.out.println(statusCode);
String newHost = "";
if (method.getResponseHeader("Location") != null) {
if (method.getResponseHeader("Location").getValue().startsWith("http")) {
String redirectUrl = method.getResponseHeader("Location").getValue();
newHost = redirectUrl.toString();
newHost = newHost.split("/")[2];
} else {
newHost = method.getURI().toString();
newHost = newHost.split("/")[2];
String redirectUrl = "http://" + newHost + method.getResponseHeader("Location").getValue();
}
}
responseStr = crawlHttpClientHelper.doGet("http://"+newHost+"/webmail/forwardlogin.jsp", "",
"http://hermes.webmail.21cn.com/webmail/login.perform?key=1");
System.out.println(responseStr);
responseStr = crawlHttpClientHelper.doGet("http://"+newHost+"/webmail/logon.do?uud=1", "",
"http://"+newHost+"/webmail/forwardlogin.jsp");
System.out.println(responseStr);
referer = "http://"+newHost+"/webmail/signOn.do";
String result1 = "http://passport.21cn.com/api/getUser.do?jsoncallback=jsonp" + System.currentTimeMillis() + "&require=pswdProtect";
responseStr = crawlHttpClientHelper.doGet(result1, "", "");
responseStr = StringUtils.substringBetween(responseStr, "isLogin\":\"", "\",");
if (!responseStr.equals("true")) {
throw new CrawlException("登录失败");
}
String searchEmailUrl = "http://"+newHost+"/webmail/mailList.do?label=1";
referer = "http://hermesw8.mail.21cn.com/webmail/navigator.do?sid=null";
responseStr = crawlHttpClientHelper.doGet(searchEmailUrl, "", referer);
if(Validate.isEmpty(responseStr)){
throw new RuntimeException("获取邮箱的内容为空");
}