HttpListener在MonoTouch上使用HTTPS

我使用MonoTouch中的HttpListener实现了一个非常简单的Web服务器。 一切工作正常。 现在我需要添加HTTPS支持。 我试图按照步骤

Httplistener与https支持

但我不知道在MonoTouch中设置证书的位置。 只要添加前缀“https:// *:443”就没有帮助,因为不可能有连接,也不会引发exception。

根据http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx ,这可能是因为必须指定一个服务器证书(“您可以configuration服务器证书和其他侦听器选项通过使用HttpCfg.exe“)。

我如何在MonoTouch中做到这一点?

这个问题问得好。 在某些情况下,如HttpListener ,.NET需要工具或.config文件(使用System.Configuration )来调整应用程序的configuration。 在许多情况下,API都达到相同的目的,但并不总是(在这种情况下)。

解决scheme是查看Mono的源代码,以查看它希望为应用程序设置HttpCfg.exe工具。 来自github :

 string dirname = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); string path = Path.Combine (dirname, ".mono"); path = Path.Combine (path, "httplistener"); string cert_file = Path.Combine (path, String.Format ("{0}.cer", port)); if (!File.Exists (cert_file)) return; string pvk_file = Path.Combine (path, String.Format ("{0}.pvk", port)); if (!File.Exists (pvk_file)) return; cert = new X509Certificate2 (cert_file); key = PrivateKey.CreateFromFile (pvk_file).RSA; 

因此,解决scheme是创build相同的目录结构(因为它将指向Documents目录下),并复制.cer文件(二进制DER编码的证书)和.pvk文件(这是私钥,格式为makecert创build)与端口号作为文件名。

使用这些文件,您应该能够启动HttpListener ,并使其加载处理SSL请求所需的必需证书和私钥。