0

我正在尝试(使用 Windows 窗体应用程序)连接到sec-websocket-protocol用于身份验证的套接字。在浏览器中一切正常,但是当我尝试使用 c# 模拟该连接时,它失败并出现错误Includes no Sec-WebSocket-Protocol header, or it has an invalid value.。日志文件:

20/11/2020 6:24:55 PM|Debug|WebSocket.sendHttpRequest:0|A request to the server:
                            GET /?encoding=text HTTP/1.1
                            User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0
                            Host: ------.com
                            Upgrade: websocket
                            Connection: keep-alive, Upgrade
                            Sec-WebSocket-Key: M3Ce------tUGg==
                            Sec-WebSocket-Version: 13
                            Accept: * / *
                            Accept-Encoding: gzip, deflate, br
                            Accept-Language: el-GR,el;q=0.9,en;q=0.8
                            Cache-Control: no-cache
                            Origin: https://--------.com
                            Pragma: no-cache
                            Sec-WebSocket-Extensions: permessage-deflate
                            Sec-WebSocket-Protocol: 549b4ftghj423f4a26------------------
20/11/2020 6:24:55 PM|Debug|WebSocket.sendHttpRequest:0|A response to this request:
                            HTTP/1.1 101 Switching Protocols
                            Upgrade: websocket
                            Connection: Upgrade
                            Sec-WebSocket-Accept: J2344x5eH------
                            Sec-WebSocket-Protocol: 549b4ftghj423f4a26----------------
20/11/2020 6:24:55 PM|Fatal|WebSocket.connect:0|WebSocketSharp.WebSocketException: Includes no Sec-WebSocket-Protocol header, or it has an invalid value.
                               at WebSocketSharp.WebSocket.doHandshake()
                               at WebSocketSharp.WebSocket.connect()
20/11/2020 6:24:55 PM|Debug|WebSocket.closeHandshake:0|Was clean?: False
                              sent: False
                              received: False

Sec-WebSocket-Protocolvalue 是我从另一个获得的代码。API我认为我从 api 正确获取了值。我多次检查过。

这是我的 C# 代码:

ws2 = new WebSocket("wss://" + "---------.com" + "/?encoding=text");

                ws2.Log.Level = LogLevel.Debug;
                ws2.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
                
                ws2.CustomHeaders = new Dictionary<string, string>
                    {
                            { "Accept", "*/*" },
                            { "Accept-Encoding", "gzip, deflate, br" },
                            { "Accept-Language", "el-GR,el;q=0.9,en;q=0.8" },
                            { "Cache-Control", "no-cache" },
                            { "Connection", "keep-alive, Upgrade" },
                            { "Origin", "https://---------.com" },
                            { "Pragma", "no-cache" },
                            { "Sec-WebSocket-Protocol", "549b4ftghj423f4a26------------------" },
                            { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0" }
                    };

                ws2.Origin = "https://---------.com" ;


                ws2.OnOpen += (sender, e) =>
                {
                    ConsoleWriteLine("OnOpen");
                };
                ws2.OnError += (sender, e) =>
                {
                    ConsoleWriteLine("OnError: " + e.Message);
                };
                ws2.OnClose += (sender, e) =>
                {
                    ConsoleWriteLine("OnClose");
                };
                ws2.OnMessage += (sender, e) =>
                    ConsoleWriteLine("Laputa says: " + e.Data);

                ws2.Connect();

设置错误是否可能Sec-WebSocket-Protocol改变值SslConfiguration。(我不确定我应该使用System.Security.Authentication.SslProtocols.Tls12)?

我试过:
1-Sec-WebSocket-Protocol从标题中删除-->响应错误:HEADER_AUTH_WS or HEADER_WEB_SOCKET_SUB_PROTOCOL is empty
2-删除设置的行SslConfiguration.EnabledSslProtocols-->没有任何改变。

4

0 回答 0