【问题标题】:How do I enable https with httplistener?如何使用 httplistener 启用 https?
【发布时间】:2018-11-06 02:50:26
【问题描述】:

我正在尝试在 Unity3D 游戏中用 c# 创建一个简单的 https 服务器,以便通过网络浏览器访问。我已经用 openssl 创建了一个服务器证书和密钥,但是我找不到一种多平台的方式来将证书传递给服务器,而无需在代码之外进行任何额外的配置。

我能找到的大部分信息都属于以下类别:

  • 使用 SslStream,但这似乎只与 TcpListener 相关(而且我想要可以提供网页的更高级别的东西)
  • 需要外部仅限 Windows 的工具,例如我不想使用的 httpcfg
  • 以编程方式或手动在证书存储中安装证书,这似乎要求程序或用户具有管理员/root 权限

我知道在 python 中你会这样做:

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

...但是在 c# 中似乎没有等效的 httplistener 或 system.security.securitymanager 或任何东西。我假设/希望我只是在这里遗漏了一些明显的东西。

对于它的价值,这是我目前所拥有的,这只是放在 Unity 脚本中的 MSDN httplistener 示例:

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 () {

    }
}

【问题讨论】:

  • 上面的评论显示了如何在 Windows 上注册证书。如果您需要支持 Mac 或 Linux,则必须将证书安装到 Mono 文件夹中。我在 Jexus Manager 远程服务中有一些示例代码,github.com/jexuswebserver/jxmgr/blob/master/RemoteServices/…
  • @LexLi 你能写一个关于如何安装它的答案吗?这会很有帮助,因为这是 Unity,我认为没有适用于 Mac 和 Linux 的现有解决方案。
  • 非常感谢您的帮助。但是,据推测这是重复的链接问题没有可接受的答案,并且除了一个解决方案之外的所有解决方案都使用 httpcfg 和/或 netsh。另一个答案使用仅限 Windows 的 API。我更改了问题的标题,以澄清我真的在这里寻找 Mono 解决方案。
  • 我最近使用 HTTPListener 实现了 HTTPS,并通过添加防火墙规则允许通过 LAN 进行通信。不需要任何输入,一切都由 C# 代码处理。我认为它可以帮助你。我在这里分享了我的解决方案:stackoverflow.com/a/58149405/983548

标签: c# unity3d https mono httplistener


【解决方案1】:

如果您从 Google 来到这里,试图在 Mono 和 SSL 中查找有关 HttpListener 的信息,您将找到有关此相关问题的更多相关信息:

Mono HttpListener client certificate

OP 最初想要的是一个简单的 Web 服务器,没有任何特定于平台的配置。到目前为止,我发现唯一支持避免平台配置的方法的库是 Ceen HTTPd。

这里有一个类似需求的海报讨论了 Ceen: https://softwarerecs.stackexchange.com/questions/52304/net-library-for-running-an-embedded-selfhosted-light-web-server-from-c-with-ss

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 2012-07-09
    • 2016-08-22
    • 2014-11-27
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多