【发布时间】:2010-11-06 13:33:51
【问题描述】:
我正在尝试用 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;
}
}
等等……
只有我不断收到异常: “对 SSPI 的调用失败,请参阅内部异常” 内部异常 -> “收到的消息意外或格式错误。”
有人知道这里出了什么问题吗?
【问题讨论】:
-
想通了。替换 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");使用 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);并在PC上注册证书。
-
是的,我一切正常,可以将消息推送到我的 iPhone。不要忘记在 windows 上注册你的 .p12 文件。
-
你在 iPhone 上运行单声道吗?
-
你应该回答你自己的问题来关闭它。
-
@Zenox - 注册 .p12 文件是必要的步骤吗?我想既然你是从文件中加载它,那没关系。
标签: c# iphone push-notification