2012-03-21 50 views
0

您好我有以下代码是应该c2dm到Android设备。当我在我的本地机器上运行它,我得到一个错误C2DM不被发送 - 没有主题替代DNS名称匹配android.clients.google.com发现

stating: Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching android.clients.google.com found.

有谁

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 
+0

这里可能是个重复的问题。你有没有尝试定义主机名验证器? http://stackoverflow.com/questions/6296547/c2dm-ioexception-when-sending-message – DavidB 2012-03-21 03:04:07

+0

是的,同意。我尝试了另一种解决方案,但是HostnameVerfier,SSLSession都是折旧的。 – sap 2012-03-21 14:09:00

回答

相关问题