2012-06-20 56 views
4

我試圖使用Java的PNS推送通知發送到iPhone,但我收到以下錯誤......javax.net.ssl.SSLHandshakeException:收到致命警報:handshake_failure錯誤APN

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 

這是我的代碼...

String token="95076d2846e8979b46efd1884206a590d99d0f3f6139d947635ac4186cdc5942"; 

String host = "gateway.sandbox.push.apple.com"; 
int port = 2195; 
String payload = "{\"aps\":{\"alert\":\"Message from Java o_O\"}}"; 

NotificationTest.verifyKeystore("res/myFile.p12", "password", false); 
KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
keyStore.load(getClass().getResourceAsStream("res/myFile.p12"), "password".toCharArray()); 

KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509"); 
keyMgrFactory.init(keyStore, "password".toCharArray()); 

SSLContext sslContext = SSLContext.getInstance("TLS"); 
sslContext.init(keyMgrFactory.getKeyManagers(), null, null); 
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); 

SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port); 
String[] cipherSuites = sslSocket.getSupportedCipherSuites(); 
sslSocket.setEnabledCipherSuites(cipherSuites); 
sslSocket.startHandshake(); 

char[] t = token.toCharArray(); 
byte[] b = Hex.decodeHex(t); 

OutputStream outputstream = sslSocket.getOutputStream(); 

outputstream.write(0); 
outputstream.write(0); 
outputstream.write(32); 
outputstream.write(b); 
outputstream.write(0); 
outputstream.write(payload.length()); 
outputstream.write(payload.getBytes()); 

outputstream.flush(); 
outputstream.close(); 

System.out.println("Message sent .... "); 

對於NotificationTest.verifyKeystore我得到這個有效的是FileKeystore

我不理解爲什麼我得到這個錯誤。

這是我的錯誤日誌...

** CertificateRequest 
Cert Types: RSA, DSS, ECDSA 
Cert Authorities: 
<empty> 
[read] MD5 and SHA1 hashes: len = 10 
0000: 0D 00 00 06 03 01 02 40 00 00 [email protected] 
** ServerHelloDone 
[read] MD5 and SHA1 hashes: len = 4 
0000: 0E 00 00 00 .... 
** Certificate chain 
** 
** ClientKeyExchange, RSA PreMasterSecret, TLSv1 
[write] MD5 and SHA1 hashes: len = 269 
... 
main, READ: TLSv1 Alert, length = 2 
main, RECV TLSv1 ALERT: fatal, handshake_failure 
main, called closeSocket() 
main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 

我不理解爲什麼當局證書是空的?

+0

正如[本答案](http://stackoverflow.com/a/11050271/372643)中所述,請不要啓用所有*支持*密碼套件。其中一些由默認情況下未啓用支持,因爲它們不安全。 – Bruno

回答

-3

發送推送通知到iPhone/iPad我已經使用JavaPNS

這是非常容易使用,它爲我工作。

我們只需關注This即可使用它。

4

我建議您使用keytool -list將客戶機上的密鑰庫與服務器已知的密鑰庫進行比較。您收到的握手錯誤是因爲服務器已經完成了問候,並期待客戶端證書的回覆。你沒有發送一個。要解決這個問題,應該將PKCS12證書轉換爲PEM格式(使用openssl是一種方式),然後使用keytool導入到密鑰庫中。

我懷疑,如果你通過將密鑰庫導入客戶端證書來解決這個問題,那麼你將會遇到第二個錯誤。第二個錯誤是關於空的CA證書 - 可能是因爲您的密鑰庫中沒有服務器已知的CA證書。導入您的CA並重試。

+0

謝謝snow6oy的回覆。我會試試看。 – Deepu

0

看起來您需要安裝「Java加密擴展(JCE)無限強度管轄權策略文件」。這解決了我的問題。

相關問題