C2DM未发送 - 找不到与主题备用DNS名称匹配的android.clients.google.com

时间:2012-03-21 02:54:38

标签: google-app-engine android-c2dm

您好我有以下代码应该c2dm到Android设备。当我在本地计算机上运行它时,出现错误

  

陈述:线程中的异常" main"   javax.net.ssl.SSLHandshakeException:   java.security.cert.CertificateException:没有主题备用DNS   名称匹配找到android.clients.google.com。

有没有人

public class MessageUtil 
{
    private final static String AUTH = "authentication";
    private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
    public static final String PARAM_REGISTRATION_ID = "registration_id";
    public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
    public static final String PARAM_COLLAPSE_KEY = "collapse_key";
    private static final String UTF8 = "UTF-8";


    public static void main(String args[]) throws IOException
    {
        String auth_token=AuthenticationUtil.token;
        String registrationId="c2dmregistration id of the device";
        String message_title="Test";
        int response=sendMessage(auth_token, registrationId,message_title);
        System.out.println(response);
    }
    public static int sendMessage(String auth_token, String registrationId,String message_title) throws IOException 
    {

        URL url = new URL("https://android.clients.google.com/c2dm/send");  

        StringBuilder postDataBuilder = new StringBuilder();
        postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
        postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
        postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message_title, UTF8));
        byte[] postData = postDataBuilder.toString().getBytes(UTF8);
        HttpURLConnection conn= (HttpURLConnection)url.openConnection(); 
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="+ auth_token);

        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();

        int responseCode = conn.getResponseCode();
        return responseCode;
    }

}

当我运行此代码时,我收到以下错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching android.clients.google.com found.
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at com.metalclub.mobile.inbox.MessageUtil.sendMessage(MessageUtil.java:45)
    at com.metalclub.mobile.inbox.MessageUtil.main(MessageUtil.java:24)
Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching android.clients.google.com found.
    at sun.security.util.HostnameChecker.matchDNS(Unknown Source)
    at sun.security.util.HostnameChecker.match(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 14 more

1 个答案:

答案 0 :(得分:0)

您正在将消息发送到错误的API端点。正确的网址为https://android.apis.google.com/c2dm/send