无法通过TLS连接到HttpListener

时间:2019-10-13 14:01:56

标签: .net tls1.2 httplistener

我在虚拟机中的本地服务器上运行HttpListener .exe

  • 当我使用Web浏览器从客户端进行连接时,它无法在TLS上运行。 (未找到资源)
  • 当我使用dotnet应用程序(HttpWebRequest)从客户端连接时,它确实可以通过TLS起作用。
  • 证书配置正确,因为我在同一台VM服务器上使用IIS托管网站测试了它们。而且效果很好。

收听者代码:


           String[] prefixes = { "https://+:8443/", "http://+:8480/" };

            // Create a listener.
            HttpListener listener = new HttpListener();
            // Add the prefixes.
            foreach (string s in prefixes)
            {
                listener.Prefixes.Add(s);
            }
            listener.Start();

            Console.WriteLine("Listening...");
            // Note: The GetContext method blocks while waiting for a request. 
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;


            // Obtain a response object.
            HttpListenerResponse response = context.Response;

            // Construct a response.
            string responseString = "<HTML><BODY> Hello world SSL!</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            // Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            // You must close the output stream.
            output.Close();
            listener.Stop();

0 个答案:

没有答案