简体   繁体   中英

How do I enable https with httplistener?

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.

Most of the info I've been able to find falls into these categories:

  • Uses SslStream, but that seems to only be relevant for TcpListener (and I want something higher level that can serve webpages)
  • Requires external Windows-only tools like httpcfg that I'd prefer not to use
  • Programmatically or manually installing certs in certificate stores, which seems to require either the program or the user to have admin/root privileges

I know in python you do something like:

ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)

...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.

For what it's worth, here's what I have so far, which is just the MSDN httplistener example put in a Unity script:

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 = "<HTML><BODY> Hello world!</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();
    }

    // Use this for initialization
    void Start () {
        String[] prefixes = { "http://*:8089/", "https://*:8443/" };
        StartServer(prefixes);
    }

    // Update is called once per frame
    void Update () {

    }
}

If you came here from Google trying to find information on HttpListener in Mono and SSL, you will find more pertinent information on this related question:

Mono HttpListener client certificate

The OPs originally desire is a simple web server without any platform-specific configuration. So far, the only library I've found that supports an approach that avoids platform configuration is Ceen HTTPd.

There was a discussion about Ceen from a poster with a similar need here: https://softwarerecs.stackexchange.com/questions/52304/net-library-for-running-an-embedded-selfhosted-light-web-server-from-c-with-ss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM