简体   繁体   English

UWP StreamSocket未接收TLS Alert(21)数据

[英]UWP StreamSocket not receiving TLS Alert (21) data

I'm trying to write an UWP app that communicates with a server using MQTT over HTTPS. 我正在尝试编写一个UWP应用程序,它通过HTTPS使用MQTT与服务器通信。 I use StreamSocket to send the whole MQTT packet over the wire but I couldn't get any response from server. 我使用StreamSocket通过线路发送整个MQTT数据包,但我无法从服务器获得任何响应。 If I attempt to resend the packet, the server would terminate the connection. 如果我尝试重新发送数据包,服务器将终止连接。 Using Wireshark, I can see that the server responded with a message Content-Type: Alert (21) with 26 bytes of data in it but I couldn't read it through StreamSocket.InputStream 使用Wireshark,我可以看到服务器响应消息Content-Type:Alert(21),其中包含26个字节的数据但我无法通过StreamSocket.InputStream读取它

var streamSocket = new StreamSocket();
Buffer packet;

// Building Mqtt packet.

await streamSocket.ConnectAsync(new HostName("server.com"), "443", SocketProtectionLevel.Tls12);
await streamSocket.OutputStream.WriteAsync(packet);

await Task.Delay(2000);  // Give the server some time to respond
var inputBytes = new byte[2048];
var completion = await streamSocket.InputStream.ReadAsync(inputBytes.AsBuffer(),(uint) inputBytes.Length, InputStreamOptions.Partial);

// inputBytes still empty

I want to be able to read the bytes server responded. 我希望能够读取服务器响应的字节数。 I think there is a way to access these bytes but I could not find it anywhere. 我认为有一种方法可以访问这些字节,但我无法在任何地方找到它。

Update: Added Wireshark result 更新:添加了Wireshark结果

Wireshark result Wireshark结果

It seems that the problem is related with the HTTPS. 似乎问题与HTTPS有关。 Have you tried to use the UpgradeToSslAsync method, if your SSL/TLS connection is desired after sending and receiving some initial data, please use the UpgradeToSslAsync method to upgrade the connection to use SSL as following: 您是否尝试使用UpgradeToSslAsync方法,如果在发送和接收某些初始数据后需要SSL / TLS连接,请使用UpgradeToSslAsync方法升级连接以使用SSL,如下所示:

await streamSocket.ConnectAsync(new HostName("server.com"), "443", Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket);
await streamSocket.UpgradeToSslAsync(Windows.Networking.Sockets.SocketProtectionLevel.Tls12, hostName);

Thanks, 谢谢,

Amy Peng 艾米彭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM