2013-03-09 2 views
2
using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 
using System.Text; 
using System.Security.Authentication; 
using System.Net.Security; 
using System.Net.Sockets; 
using System.Security.Cryptography.X509Certificates; 
using Newtonsoft.Json.Linq; 

namespace WebApplication1 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void btnPush_Click(object sender, EventArgs e) 
     { 
      pushMessage(txtDeviceID.Text.Trim(), txtPayload.Text.Trim()); 
     } 


     public void pushMessage(string deviceID, string Mesaj) 
     { 

      int port = 2195; 
      String hostname = "ssl://gateway.sandbox.push.apple.com:2195"; 

      String certificatePath = HttpContext.Current.Server.MapPath("PushNotifi.p12"); 
      X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "taxmann"); 
      X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate); 

      TcpClient client = new TcpClient(hostname, port); 
      SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 

      try 
      { 
       sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Ssl3, false); 

       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\":{\body\":\"" + Mesaj + "\"},\"badge\":1,\"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(); 
       lblResponse.Text = "Sucess.."; 
      } 
      catch (System.Security.Authentication.AuthenticationException ex) 
      { 
       client.Close(); 
       lblResponse.Text = ex.Message; 
      } 
      catch (Exception e) 
      { 
       client.Close(); 
       lblResponse.Text = e.Message; 
      } 
     } 

     // The following method is invoked by the RemoteCertificateValidationDelegate. 
     public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
     { 
      if (sslPolicyErrors == SslPolicyErrors.None) 
       return true; 
      else // Do not allow this client to communicate with unauthenticated servers. 
       return false; 
     } 

     private static byte[] HexStringToByteArray(String DeviceID) 
     { 
      //convert Devide token to HEX value. 
      byte[] deviceToken = new byte[DeviceID.Length/2]; 
      for (int i = 0; i < deviceToken.Length; i++) 
       deviceToken[i] = byte.Parse(DeviceID.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); 

      return deviceToken; 
     } 
    } 
} 

이것은 내 코드입니다. 나는이 프로그램을 실행할 때 사용자 코드에 의해 처리되지 않은 SocketException이

나는이 문제를 어떻게 해결할 수

system.net.sockets.socketexceptionnot recognize ssl://gateway.sandbox.push.apple.com:2195 host name 인터넷 검색을 할 때, 다음

SocketException이 사용자 코드 라인

TcpClient client = new TcpClient(host-name, port); 

에 의해 처리되지 않은입니까?

기본적으로이 코드는 iPhone 응용 프로그램에 푸시 알림을 보내기위한 것입니다.

알림을 전송하는 서버로 localhost을 사용하고 있습니다.

답변

0

나는 문제가 hostname 문자열 생각 :

포트 값 (2195)
int port = 2195; 
String hostname = "ssl://gateway.sandbox.push.apple.com:2195"; 
TcpClient client = new TcpClient(hostname, port); 

별도의 생성자 매개 변수, 그래서 당신이 호스트에 전달해야한다고 생각하지 않습니다 매개 변수, 도 마찬가지입니다..

또한, 프로토콜 (예컨대 ssl://)는 호스트 의 일부이어야한다. SslStream은 .NET에 SSL임을 알립니다.

int port = 2195; 
String hostname = "gateway.sandbox.push.apple.com"; 
TcpClient client = new TcpClient(hostname, port); 

추신 : 당신이 this similar question on stack overflow 보면

, 당신은 그들이 같은 것을 사용하는 것을 볼 수 있습니다 나도 너에게 맞았다 고 생각해. in your original question on this subject

관련 문제