Tôi đang cố gắng viết một máy chủ đẩy cho iPhone trong C#. Tôi có đoạn mã sau:C# iPhone đẩy máy chủ?
// 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;
}
}
vv ....
Chỉ có tôi tiếp tục nhận được một ngoại lệ: "Một cuộc gọi đến SSPI thất bại, xem ngoại lệ bên trong" Nội ngoại lệ -> "Thông điệp nhận được bất ngờ hoặc bị định dạng sai. "
Có ai có ý tưởng gì không ổn ở đây không?
Đã tìm ra. Đã thay thế sslStream.AuthenticateAsClient ("gateway.sandbox.push.apple.com"); với sslStream.AuthenticateAsClient ("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); Và đã đăng ký chứng chỉ trên PC. – Kyle
Nó phù hợp với bạn? – Zanoni
Có, tôi có mọi thứ hoạt động và có thể đẩy tin nhắn vào iPhone của mình. Đừng quên đăng ký tập tin .p12 của bạn với các cửa sổ. – Kyle