C# 如何使用httplistener启用https?

C# 如何使用httplistener启用https?,c#,unity3d,https,mono,httplistener,C#,Unity3d,Https,Mono,Httplistener,我试图在Unity3D游戏中用c#创建一个简单的https服务器,通过web浏览器访问。我已经用openssl创建了一个服务器证书和密钥,但是我找不到一种多平台的方法将证书传递到服务器,而不需要代码之外的任何额外配置 我能找到的大部分信息都分为以下几类: 使用SslStream,但这似乎只与TcpListener相关(我想要更高级别的东西来服务网页) 只需要外部Windows工具,如httpcfg,我不希望使用 以编程方式或手动方式在证书存储中安装证书,这似乎要求程序或用户具有管理员/root

我试图在Unity3D游戏中用c#创建一个简单的https服务器,通过web浏览器访问。我已经用openssl创建了一个服务器证书和密钥,但是我找不到一种多平台的方法将证书传递到服务器,而不需要代码之外的任何额外配置

我能找到的大部分信息都分为以下几类:

  • 使用SslStream,但这似乎只与TcpListener相关(我想要更高级别的东西来服务网页)
  • 只需要外部Windows工具,如httpcfg,我不希望使用
  • 以编程方式或手动方式在证书存储中安装证书,这似乎要求程序或用户具有管理员/root权限
我知道在python中,您可以执行以下操作:

ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)
…但在c#for httplistener中,或者在system.security.securitymanager或任何东西中,似乎都没有一个等价物。我想/希望我只是错过了一些明显的东西

值得一提的是,到目前为止,我所拥有的只是统一脚本中的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 () {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用制度;
Net系统;
公共类SimpleListenerExample:MonoBehavior{
//此示例需要System和System.Net命名空间。
公共静态void StartServer(字符串[]前缀)
{
如果(!HttpListener.IsSupported)
{
WriteLine(“需要Windows XP SP2或Server 2003才能使用HttpListener类。”);
返回;
}
//URI前缀是必需的,
//例如“http://contoso.com:8080/index/".
if(前缀==null | |前缀.长度==0)
抛出新的ArgumentException(“前缀”);
//创建一个侦听器。
HttpListener=新的HttpListener();
//添加前缀。
foreach(前缀中的字符串s)
{
listener.Prefixes.Add(s);
}
/*这是我加载服务器证书的部分…不知怎么的*/
listener.Start();
Console.WriteLine(“监听…”);
//注意:GetContext方法在等待请求时阻塞。
HttpListenerContext=listener.GetContext();
HttpListenerRequest=context.request;
//获取一个响应对象。
HttpListenerResponse=context.response;
//构建一个响应。
string responseString=“你好,世界!”;
byte[]buffer=System.Text.Encoding.UTF8.GetBytes(responseString);
//获取响应流并将响应写入其中。
response.ContentLength64=buffer.Length;
System.IO.Stream输出=response.OutputStream;
输出.写入(缓冲区,0,缓冲区.长度);
//必须关闭输出流。
output.Close();
listener.Stop();
}
//用于初始化
无效开始(){
字符串[]前缀={“http://*:8089/”,“https://*:8443/”;
StartServer(前缀);
}
//每帧调用一次更新
无效更新(){
}
}

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

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

这里有一个关于Ceen的讨论,来自一个有类似需求的海报:

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