Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 本地服务器上的HTML响应乱七八糟_C#_Http_Tcplistener - Fatal编程技术网

C# 本地服务器上的HTML响应乱七八糟

C# 本地服务器上的HTML响应乱七八糟,c#,http,tcplistener,C#,Http,Tcplistener,使用http代替https解决了这个问题。谢谢Kalten 这是我使用.Net TcpListener创建的第一个HTTP服务器。我的代码如下所示: TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 44394); tcpListener.Start(); while (true) { Console.WriteLin

使用http代替https解决了这个问题。谢谢Kalten

这是我使用.Net TcpListener创建的第一个HTTP服务器。我的代码如下所示:

TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 44394);
            tcpListener.Start();

            while (true)
            {
                Console.WriteLine("Listening...");

                TcpClient client = tcpListener.AcceptTcpClient();
                using (NetworkStream stream = client.GetStream())
                {
                    byte[] requestBytes = new byte[100000];

                    int readBytes = stream.Read(requestBytes, 0, requestBytes.Length);

                    var requestResult = Encoding.UTF8.GetString(requestBytes,0,readBytes);
                    Console.WriteLine(requestResult);

                    Console.WriteLine(new string('>',50)); //for splitting responses

                }

            }   

用于发送带有url的请求浏览器的客户端:

它返回的字符串如下所示:

TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 44394);
            tcpListener.Start();

            while (true)
            {
                Console.WriteLine("Listening...");

                TcpClient client = tcpListener.AcceptTcpClient();
                using (NetworkStream stream = client.GetStream())
                {
                    byte[] requestBytes = new byte[100000];

                    int readBytes = stream.Read(requestBytes, 0, requestBytes.Length);

                    var requestResult = Encoding.UTF8.GetString(requestBytes,0,readBytes);
                    Console.WriteLine(requestResult);

                    Console.WriteLine(new string('>',50)); //for splitting responses

                }

            }   

�u$�15�Q��νѴ��Q�~YWzw4� ��}� U�8BЎeJ��43x���;��?��d@���~ “ZZ�+�/�,�0̨̩�� � � / 5. ��� 本地主机�

�� # h2http/1.1
3 + )�� �d{�P��{���|�聚氨基甲酸酯����;^ܑ��/ - + zz::�

我尝试过的事情: 1.使用在线字节到utf-8转换器手动解码字节数组(我发现错误,这些字节无法转录到utf-8)
2.尝试使用Gzip解压(返回错误,内容不是Gzip)

客户端发送了什么?服务器不“返回”"无中生有的请求。某些客户端必须发送一个请求,您可以接收并显示该请求。请准确解释您使用的客户端以及您使用该客户端的方式。我使用了chrome并刚刚输入了url:您正在尝试使用httpS协议调用服务器。它需要ssl/tls加密和相关证书。请尝试使用http协议或使用