C# C语言中的Apple推送通知服务器端#
我是.net新手,在C#中做apn服务器端。我使用下面的代码将消息推送到苹果服务器C# C语言中的Apple推送通知服务器端#,c#,.net,apple-push-notifications,C#,.net,Apple Push Notifications,我是.net新手,在C#中做apn服务器端。我使用下面的代码将消息推送到苹果服务器 private void pushMessage() { int port = 2195; String deviceID = "4564c705 63b371aa 3811699e 1e4ac3d2 ba592b27 f2a5a613 d25cd035 xx213e54"; String hostname = "gateway.sandbox.pu
private void pushMessage()
{
int port = 2195;
String deviceID = "4564c705 63b371aa 3811699e 1e4ac3d2 ba592b27 f2a5a613 d25cd035 xx213e54";
String hostname = "gateway.sandbox.push.apple.com"; // TEST
//String hostname = "gateway.push.apple.com"; // REAL
// @"cert.p12";
String certificatePath = HttpContext.Current.Server.MapPath("Certi.p12");
//X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, "");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
TcpClient client = new TcpClient(hostname, port);
// _apnsStream = new SslStream(_apnsClient.GetStream(), false, validateServerCertificate, SelectLocalCertificate);
//SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate),null);
SslStream sslStream = new SslStream(client.GetStream(), false, validateServerCertificate, SelectLocalCertificate);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
}
catch (Exception e)
{
throw (e);
client.Close();
return;
}
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0); //The command
writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)
writer.Write(HexStringToByteArray(deviceID.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"hello\",\"badge\":0,\"sound\":\"default\"}}";
writer.Write((byte)0);
writer.Write((byte)payload.Length);
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
client.Close();
}
private string HexStringToByteArray(string p)
{
throw new NotImplementedException();
}
在执行过程中,出现类似“调用SSPI失败,请参阅内部异常”的错误
我在这里做错了或错过了什么。windows中是否需要安装任何证书?请帮助我。我在PushNotification.class文件中更改了代码
_certificate = string.IsNullOrEmpty(p12FilePassword)? new X509Certificate2(File.ReadAllBytes(p12File)): new X509Certificate2(File.ReadAllBytes(p12File), p12FilePassword);
与
所以试试
SslStream sslStream = new SslStream(client.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
....
public static bool ValidateServerCertificate(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
gerarLog("Certificate error: " + sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
public static byte[] HexStringToByteArray(string hexString)
{
byte[] HexAsBytes = new byte[hexString.Length / 2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return HexAsBytes;
}
SslStream SslStream=新的SslStream(client.GetStream(),false,
新的RemoteCertificateValidationCallback(ValidateServerCertificate),null);
....
公共静态bool ValidateServerCertificate(对象发送方,
X509证书,
X509链条,
SslPolicyErrors SslPolicyErrors)
{
如果(sslPolicyErrors==sslPolicyErrors.None)
返回true;
gerarLog(“证书错误:+sslPolicyErrors”);
//不允许此客户端与未经身份验证的服务器通信。
返回false;
}
公共静态字节[]HexStringToByteArray(字符串hexString)
{
byte[]HexAsBytes=新字节[hexString.Length/2];
for(int index=0;index
您需要单击查看详细信息…并告诉我们内部异常详细信息是什么。您的代码也有一些问题-例如您的捕获
是无用的-它只会丢失堆栈跟踪,并且抛出(e)
之后的任何代码都不会执行(举几个例子)。我现在要做什么?请简单解释一下。看一看。OP为他们如何解决同一问题提供了答案。我强烈建议使用,因为他们有工作示例,您可以简单地获取和使用…您知道您的异常处理程序是错误的,对吗?客户端永远不会关闭,因为抛出(e)代码>执行从异常处理程序的即时退出,因此将永远不会调用以下两行,并且异常将冒泡。标记为低质量。请改进一下。
SslStream sslStream = new SslStream(client.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
....
public static bool ValidateServerCertificate(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
gerarLog("Certificate error: " + sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
public static byte[] HexStringToByteArray(string hexString)
{
byte[] HexAsBytes = new byte[hexString.Length / 2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return HexAsBytes;
}