title: How do I enable https with httplistener?
body: <p>I am trying to create a simple https server in c# in a Unity3D game, to be accessed through a web browser. I've created a server cert and key with openssl, but I can't find a multi-platform way to pass the cert to the server without any additional configuration outside the code.</p>
  
  <p>Most of the info I've been able to find falls into these categories:</p>
  
  <ul>
  <li>Uses SslStream, but that seems to only be relevant for TcpListener (and I want something higher level that can serve webpages)</li>
  <li>Requires external Windows-only tools like httpcfg that I'd prefer not to use</li>
  <li>Programmatically or manually installing certs in certificate stores, which seems to require either the program or the user to have admin/root privileges</li>
  </ul>
  
  <p>I know in python you do something like:</p>
  
  <pre><code>ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)
  </code></pre>
  
  <p>...but there doesn't seem to be an equivalent in c# for httplistener, or in system.security.securitymanager or anything. I assume/hope that I'm just missing something obvious here.</p>
  
  <p>For what it's worth, here's what I have so far, which is just the MSDN httplistener example put in a Unity script:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System;
  using System.Net;
  
  public class SimpleListenerExample : MonoBehaviour {
  
      // This example requires the System and System.Net namespaces.
      public static void StartServer(string[] prefixes)
      {
          if (!HttpListener.IsSupported)
          {
              Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
              return;
          }
          // URI prefixes are required,
          // for example "http://contoso.com:8080/index/".
          if (prefixes == null || prefixes.Length == 0)
              throw new ArgumentException("prefixes");
  
          // Create a listener.
          HttpListener listener = new HttpListener();
          // Add the prefixes.
          foreach (string s in prefixes)
          {
              listener.Prefixes.Add(s);
          }
  
          /* and here's the part where I would load the server certificate ...somehow */
  
          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 = "&lt;HTML&gt;&lt;BODY&gt; Hello world!&lt;/BODY&gt;&lt;/HTML&gt;";
          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();
      }
  
      // Use this for initialization
      void Start () {
          String[] prefixes = { "http://*:8089/", "https://*:8443/" };
          StartServer(prefixes);
      }
  
      // Update is called once per frame
      void Update () {
  
      }
  }
  </code></pre>