在第一个 C# 请求通过 ssh 隧道后,第二个请求堆栈在 GetResponse 上,但浏览器请求有效

本文关键字:请求 堆栈 GetResponse 有效 浏览器 第二个 第一个 ssh 隧道 | 更新日期: 2023-09-27 18:32:46

我用这个库创建SSH隧道:https://sshnet.codeplex.com/

隧道工作正常,如果在火狐中输入网址,例如:http://localhost:10000/somefile.js

它总是被返回。但是,如果我在输出中出现第一个请求后从控制台应用程序请求它

SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'.
SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelDataMessage': 'SSH_MSG_CHANNEL_DATA : #0'.
SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelEofMessage': 'SSH_MSG_CHANNEL_EOF : #0'.
SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelEofMessage': 'SSH_MSG_CHANNEL_EOF : #0'.
SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelCloseMessage': 'SSH_MSG_CHANNEL_CLOSE : #0'.
SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelCloseMessage': 'SSH_MSG_CHANNEL_CLOSE : #0'.

和之后

'SSH_MSG_CHANNEL_EOF : #0'。

代码卡在这里:

response = request.GetResponse();

然后超时。

这是我的完整代码:

        connectionInfo = new PrivateKeyConnectionInfo(domain, name, new PrivateKeyFile(File.OpenRead(keyPath), pass));
          using (client = new SshClient(connectionInfo)) {
            client.ErrorOccurred += client_ErrorOccurred;
            client.Connect();
            port = new ForwardedPortLocal(boundHost, boundPort, remoteHost, remotePort);           
            client.AddForwardedPort(port);
            port.Exception += port_Exception;
            port.Start();
            //This request successfully returned but after this output shows SSH_MSG_CHANNEL_EOF
            SendRequest();
            Thread.Sleep(2000);
            // And in this Request is code stacked on get response
            SendRequest();
        // Request Call 
        void SendRequest(){
        string url = "http://localhost:10000/somefile.js";
        WebResponse response = null;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.KeepAlive = true;
        request.Timeout = 15000;
        // Here its stacked after 2 request
        response = request.GetResponse();
        MemoryStream stream = new MemoryStream();
        response.GetResponseStream().CopyTo(stream);
        stream.Position = 0;
        StreamReader reader = new StreamReader(stream);
        string bssResponse = reader.ReadToEnd();
        Console.WriteLine(bssResponse.Substring(0, 20));

那么来自火狐和控制台应用程序的调用之间有什么区别呢?

来自 Firefox 的请求在隧道输出中调用通道的打开和关闭,但不调用 EOF

我试图用小提琴手检查它,所以我像在火狐中一样制作了一切,但它仍然不起作用

当我通过腻子创建此隧道时 - 控制台应用程序可以工作。

感谢您的任何帮助或建议。

在第一个 C# 请求通过 ssh 隧道后,第二个请求堆栈在 GetResponse 上,但浏览器请求有效

经过两天的搜索,我找到了! 请求堆叠在端口上,通道不近,因为我忘记设置

request.KeepAlive = false;

我希望这会帮助某人