我正在嘗試使用c#代碼做蘋果推送通知。它在我的本地機器上正常工作,我收到通知。但是,當上傳到服務器時,我嘗試創建新證書時遇到了異常。這是我使用上傳到服務器時出現APN錯誤
protected void sendPayLoad(string message, string eqpt_ID)
{
try
{
if (eqpt_ID == string.Empty || eqpt_ID == " (null)")
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "No equipment ID");
return;
}
int port;
String hostname, cert_path, payload;
string deviceId, certificatePath, certificatePassword;
X509Certificate2 clientCertificate=null;
X509Certificate2Collection certificatesCollection=null;
TcpClient client=null;
SslStream sslStream=null;
MemoryStream memoryStream=null;
BinaryWriter writer=null;
byte[] b1, array;
hostname = "gateway.sandbox.push.apple.com";// "gateway.push.apple.com";
cert_path = Session["cert_path"].ToString();
port = 2195;
if (cert_path == string.Empty)
{
Alert.Show("No certificate found for sending message to customer");
return;
}
//load certificate
certificatePath = Server.MapPath(cert_path);//Add p12 certificate here
certificatePassword = string.Empty;
try
{
clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
}
catch (Exception ex)
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message);
return;
}
try
{
certificatesCollection = new X509Certificate2Collection(clientCertificate);
}
catch (Exception ex)
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2Collection error:- \t " + ex.Message);
return;
}
try
{
client = new TcpClient(hostname, port);
}
catch (Exception ex)
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "TcpClient error:- \t " + ex.Message);
return;
}
try
{
sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
}
catch (Exception ex)
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "SslStream or RemoteCertificateValidationCallback error:- \t " + ex.Message);
return;
}
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
}
catch (AuthenticationException ex)
{
client.Close();
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "AuthenticationException:\n\t" + ex.Message);
return;
}
// Encode a test message into a byte array.
memoryStream = new MemoryStream();
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)
deviceId = eqpt_ID;
writer.Write(HexToData(deviceId.ToUpper()));
payload = "{\"aps\":{\"alert\":\"" + message + "\",\"badge\":1}}";
writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
writer.Write((byte)payload.Length); //payload length (big-endian second byte)
b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
//String deviceId = "DEVICEIDGOESHERE";
// Close the client connection.
client.Close();
}
catch (Exception ex)
{
Cre_Log_Err_takeaway.Append_Err_Msg("rest/booking.aspx_page", "void sendPayLoad(string message, string eqpt_ID)", ex.Message);
}
}
雖然試圖在服務器上運行我正在一個excception在該行
try
{
clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
}
catch (Exception ex)
{
Log_Error_Message("void sendPayLoad(string message, string eqpt_ID)", "X509Certificate2 error:- \t " + ex.Message);
return;
}
在日誌中,我只得到
的內部代碼發生錯誤
我已打印出證書路徑及其指向這條道路
d:\主機\ 8842886 \ HTML \ testwebsite \ certificate_201212725953Pushcerti.p12
而且這個證書我沒有任何密碼,所以我用Sting.Empty我也嘗試通過這樣的空字符串「」。仍然沒有運氣。 我以前在活服務器中使用過這個代碼,它工作正常。這次我使用的是godaddy服務器。
我該如何正確地做到這一點?
感謝
除了您發佈你得到了什麼錯誤,例如,如果你要在服務器上調試代碼..也是你所談論的服務器上的所有代碼是否有d:驅動器你可否確認...?你還可以比較工作機器上證書與非機器證書的區別。必須有一些不同的小東西..它可能在所有應有的尊重中正視你的臉。也檢查.config文件設置或Machine.Config設置只是爲了確保.... – MethodMan 2012-02-16 20:17:06
結帳此鏈接以及它可能會幫助您找到一個快速解決方案http://social.msdn.microsoft.com/Forums/en-AU/csharplanguage/thread/4011d0ea-3dba-4d04-b6cf-adada7956f02 – MethodMan 2012-02-16 20:19:51