gpt4 book ai didi

iphone - SocketException 未被用户代码处理

转载 作者:行者123 更新时间:2023-12-01 16:51:33 32 4
gpt4 key购买 nike

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 was unhandled by user code



在线的

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

谷歌搜索时 system.net.sockets.socketexceptionnot recognize ssl://gateway.sandbox.push.apple.com:2195 host name
我怎样才能解决这个问题?

基本上,此代码用于将推送通知发送到 iPhone 应用程序。

我正在使用 localhost作为发送通知的服务器。

最佳答案

我认为问题是你的hostname字符串:

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

端口值 (2195) 是一个单独的构造函数参数,因此我认为您也不应该在主机名参数中传递它。

此外,协议(protocol)(例如 ssl:// )不应该是主机名的一部分。 SslStream告诉 .NET 这是 SSL。

如果你看 this similar question on stack overflow ,你会看到他们使用这样的东西:

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

附言我想你也猜对了, in your original question on this subject

关于iphone - SocketException 未被用户代码处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15307905/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com