2009-06-28 146 views
30

我試圖在C#中爲iPhone編寫推送服務器。我有以下代碼:C#iPhone推送服務器?

 // Create a TCP/IP client socket. 
     using (TcpClient client = new TcpClient()) 
     { 
      client.Connect("gateway.sandbox.push.apple.com", 2195); 
      using (NetworkStream networkStream = client.GetStream()) 
      { 
       Console.WriteLine("Client connected."); 

       X509Certificate clientCertificate = new X509Certificate(@"certfile.p12", passwordHere); 
       X509CertificateCollection clientCertificateCollection = new X509CertificateCollection(new X509Certificate[1] { clientCertificate }); 

       // Create an SSL stream that will close the client's stream. 
       SslStream sslStream = new SslStream(
        client.GetStream(), 
        false, 
        new RemoteCertificateValidationCallback(ValidateServerCertificate), 
        null 
        ); 

       try 
       { 
        sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com"); 
       } 
       catch (AuthenticationException e) 
       { 
        Console.WriteLine("Exception: {0}", e.Message); 
        if (e.InnerException != null) 
        { 
         Console.WriteLine("Inner exception: {0}", e.InnerException.Message); 
        } 
        Console.WriteLine("Authentication failed - closing the connection."); 
        client.Close(); 
        return; 
       } 
      } 

ECT ....

只有我不斷收到一個例外: 「到SSPI調用失敗,請參見內部異常」 內部異常 - >「收到的消息出乎意料或格式不正確。「

有沒有人有任何想法這裏怎麼了?

+6

想通了。替換爲 sslStream.AuthenticateAsClient(「gateway.sandbox.push.apple.com」); with sslStream.AuthenticateAsClient(「gateway.sandbox.push.apple.com」,clientCertificateCollection,SslProtocols.Default,false); 並在PC上註冊證書。 – Kyle 2009-06-29 00:15:37

+0

它適合你嗎? – Zanoni 2009-07-03 17:10:14

+0

是的,我有一切工作,並可以推送消息到我的iPhone。不要忘記用windows註冊你的.p12文件。 – Kyle 2009-07-06 22:26:02

回答

8

想通了。替換sslStream.AuthenticateAsClient(「gateway.sandbox.push.apple.com」);與sslStream.AuthenticateAsClient(「gateway.sandbox.push.apple.com」,clientCertificateCollection,SslProtocols.Default,false);並在PC上註冊證書。

編輯:這是按要求建立一個有效載荷的代碼:

private static byte[] GeneratePayload(byte [] deviceToken, string message, string sound) 
    { 
     MemoryStream memoryStream = new MemoryStream(); 

     // Command 
     memoryStream.WriteByte(0); 

     byte[] tokenLength = BitConverter.GetBytes((Int16)32); 
     Array.Reverse(tokenLength); 
     // device token length 
     memoryStream.Write(tokenLength, 0, 2); 

     // Token 
     memoryStream.Write(deviceToken, 0, 32); 

     // String length 
     string apnMessage = string.Format ("{{\"aps\":{{\"alert\":{{\"body\":\"{0}\",\"action-loc-key\":null}},\"sound\":\"{1}\"}}}}", 
      message, 
      sound); 

     byte [] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length); 
     Array.Reverse (apnMessageLength); 
     // message length 
     memoryStream.Write(apnMessageLength, 0, 2); 

     // Write the message 
     memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length); 

     return memoryStream.ToArray(); 
    } // End of GeneratePayload 
1

其他方法就是使用X509Certificate2和X509CertificateCollection2類。

0

「收到的消息是意外或格式錯誤。」當您沒有在Windows中註冊p12證書時,通常會出現錯誤。 (在Vista下,剛上P12雙擊文件,然後導入嚮導將打開)

5

從Zenox的評論: 使用不同版本的AuthenticateAsClient

sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);
0

在我而言,我不得不從我的Windows 8中刪除所有的證書,然後才能發送推送通知重新安裝它們到蘋果設備。

我不知道爲什麼我的證書停止工作,我正在尋找正確的原因,並會盡快在此更新。