c# – 创建电报auth_key
我最近开始使用电报api.在第一阶段,我发出了接收auth_key的请求.
这是我的c#代码: // auth_key_id in unencrypted message is ZERO Int64 auth_key_id = 0; // this is current time stamp that used as message id Int64 message_id = DateTime.Now.Ticks; // message type for req_pq is 0x60469778 in big-ending format byte[] message_type = {120,151,70,96}; // this is data lenght,it determind in run time Int32 data_lenght; // data is combined message_type and an int128 bit value called nonce // nonce create by random byte[] nonce = new byte[16]; Random rand = new Random(1); rand.NextBytes(nonce); // make data List<byte> dataList = new List<byte>(); dataList.AddRange(message_type); dataList.AddRange(nonce); byte[] data = dataList.ToArray(); // make packet List<byte> packetList = new List<byte>(); packetList.AddRange(BitConverter.GetBytes(auth_key_id)); packetList.AddRange(BitConverter.GetBytes(message_id)); data_lenght = data.Length; packetList.AddRange(BitConverter.GetBytes(data_lenght)); packetList.AddRange(data); byte[] packet = packetList.ToArray(); try { Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); s.Connect("149.154.167.40",443); if (s.Connected) { IPEndPoint remote = s.RemoteEndPoint as IPEndPoint; Console.WriteLine("Connected To : "+remote.Address+":"+remote.Port); } int sendLength = s.Send(packet); Console.WriteLine("Send " +sendLength+" Byte(s)"); byte[] received = new byte[128]; int recLen = s.Receive(received); Console.WriteLine("Received " + recLen + " Byte(s)"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine(e.Message); } 我通过wireshark捕获发送数据并获取此有效负载:
粗体部分是我的有效载荷,根据电报文件,它似乎是正确的,但我没有从服务器得到任何响应. 解决方法
轻微评论:我对Telegram API一无所知.
首先,您将连接到149.154.167.40:443.这是HTTPS端点的默认端口,这意味着您的客户端需要“说”SSL才能进行通信. 基本上发生的是连接到端口,服务器等待有效的SSL握手.但你实际发送的是实际内容本身.服务器注意到您正在发送内容,但由于收到的字节数小于有效的SSL握手,因此它会一直等待您发送更多内容. 你要做的是“说”这个SSL协议. SSL使您发送和/或接收的内容得以加密.例如,这可以通过使用SslStream来完成.这略高一些,但它会阻止您实现SSL实现. 另一方面,您可以尝试连接到端口80(如果可用),但这会禁用加密.我不建议这样做. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |