求Java模拟登录139邮箱实例--139 protocol has changed

林晓盛 发布于 2012/09/06 14:31
阅读 1K+
收藏 1
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;
    }
}

报错如下:

com.seed.contacts.exception.ContactsException: 139 protocol has changed
at com.seed.contacts.email.EmailImporter.getContacts(EmailImporter.java:70)
at test.Test.main(Test.java:87)
Caused by: com.seed.contacts.exception.ContactsException: 139 protocol has changed
at com.seed.contacts.email.OneThreeNineImporter.doLogin(OneThreeNineImporter.java:41)
at com.seed.contacts.email.EmailImporter.getContacts(EmailImporter.java:65)
... 1 more
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:632)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at java.io.FilterOutputStream.flush(FilterOutputStream.java:123)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:506)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.seed.contacts.email.EmailImporter.doPost(EmailImporter.java:92)
at com.seed.contacts.email.OneThreeNineImporter.doLogin(OneThreeNineImporter.java:36

求解!!!!各位大大帮忙看一下哈!

加载中
返回顶部
顶部