67,513
社区成员
发帖
与我相关
我的任务
分享
package com.seed.contacts.email;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import com.seed.contacts.Contact;
import com.seed.contacts.exception.ContactsException;
/**
* 获取139邮箱联系人
* @author jeanx
*
*/
public class OneThreeNineImporter extends EmailImporter {
private String loginUrl = "https://mail.10086.cn/Login/Login.ashx";
public OneThreeNineImporter(String email, String password) {
super(email, password, "GBK");
}
/**
* 模拟登录
*/
protected void doLogin() throws ContactsException {
try {
NameValuePair[] params = {
new NameValuePair("UserName", getUsername(this.email)),
new NameValuePair("Password", this.password),
new NameValuePair("VerifyCode", "") };
String responseStr = doPost(this.loginUrl, params,"http://mail.10086.cn");
String redirectUrl = getHrefUrl(responseStr, "http://");
responseStr = doGet(redirectUrl, this.loginUrl);
} catch (Exception e) {
throw new ContactsException("139 protocol has changed", e);
}
}
/**
* 解析相应页面,获得联系人列表
*/
protected List<Contact> parseContacts() throws ContactsException {
try {
List contacts = new ArrayList();
String sid = getSid();
String randNum = "0.0754233067456439";
String contactsUrl = "http://mail.139.com/addr/apiserver/GetContactsDataByJs.ashx?sid="+ sid + "&rnd=" + randNum;
String content = doGet(contactsUrl);
JSONObject jsonObj = parseJSON(content, "GetUserAddrDataResp=");
JSONArray jsonContacts = jsonObj.getJSONArray("Contacts");
for (int i = 0; i < jsonContacts.length(); i++) {
JSONObject jsonContact = (JSONObject) jsonContacts.get(i);
String username = jsonContact.getString("c");
String email = jsonContact.getString("y");
contacts.add(new Contact(username, email));
}
return contacts;
} catch (Exception e) {
}
throw new ContactsException("139 protocol has changed");
}
/**
* 判断是否为139邮箱
* @param email
* @return
*/
public static boolean is139(String email) {
return email.indexOf("@139.") != -1;
}
/**
* 获得SID
* @return
*/
private String getSid() {
String sid = null;
Cookie[] cookies = this.client.getState().getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("Os_SSo_Sid")) {
sid = cookie.getValue();
break;
}
}
return sid;
}
}
package com.seed.contacts.email;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.seed.contacts.Contact;
import com.seed.contacts.exception.ContactsException;
/**
* 获取139邮箱联系人
* @author jeanx
*
*/
public class OneThreeNineImporter extends EmailImporter {
private String loginUrl = "https://mail.10086.cn/Login/Login.ashx";
public OneThreeNineImporter(String email, String password) {
super(email, password, "GBK");
}
/**
* 模拟登录
*/
protected void doLogin() throws ContactsException {
try {
//创建一个SSL上下文接受任何证书
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(ctx);
NameValuePair[] params = {
new NameValuePair("UserName", getUsername(this.email)),
new NameValuePair("Password", this.password),
new NameValuePair("VerifyCode", "") };
String responseStr = doPost(this.loginUrl, params,"http://mail.10086.cn");
String redirectUrl = getHrefUrl(responseStr, "http://");
responseStr = doGet(redirectUrl, this.loginUrl);
} catch (Exception e) {
throw new ContactsException("139 protocol has changed", e);
}
}
/**
* 解析相应页面,获得联系人列表
*/
protected List<Contact> parseContacts() throws ContactsException {
try {
List contacts = new ArrayList();
String sid = getSid();
String randNum = "0.7155617944761764";
String contactsUrl = "http://g1.mail.10086.cn/addr/apiserver/GetContactsDataNormal.ashx?sid="+ sid + "&rnd=" + randNum;
String content = doGet(contactsUrl);
JSONObject jsonObj = parseJSON(content, "GetUserAddrDataResp=");
JSONArray jsonContacts = jsonObj.getJSONArray("Contacts");
for (int i = 0; i < jsonContacts.length(); i++) {
JSONObject jsonContact = (JSONObject) jsonContacts.get(i);
String username = jsonContact.getString("c");
String email = jsonContact.getString("y");
contacts.add(new Contact(username, email));
}
return contacts;
} catch (Exception e) {
}
throw new ContactsException("139 protocol has changed");
}
/**
* 判断是否为139邮箱
* @param email
* @return
*/
public static boolean is139(String email) {
return email.indexOf("@139.") != -1;
}
/**
* 获得SID
* @return
*/
private String getSid() {
String sid = null;
Cookie[] cookies = this.client.getState().getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("Os_SSo_Sid")) {
sid = cookie.getValue();
break;
}
}
return sid;
}
/**
* 创建一个SSL上下文接受任何证书
* http://stackoverflow.com/questions/1828775/httpclient-and-ssl
* @author jeanx
*
*/
private static class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}