在 MonoTouch 上使用 HTTPS 的 HttpListener [英] HttpListener with HTTPS on MonoTouch
问题描述
我使用 MonoTouch 中的 HttpListener
实现了一个非常简单的 Web 服务器.一切正常.现在我需要添加 HTTPS 支持.我尝试按照
I implemented a very simple web server using the HttpListener
in MonoTouch. Everything is working fine. Now I need to add HTTPS support. I tried to follow the steps from
但我不知道在 MonoTouch 中在哪里设置证书.仅添加前缀https://*:443"没有帮助,因为不可能有连接并且不会引发异常.
but I don't know where to set the certificates in MonoTouch. Just adding the prefix "https://*:443" doesn't help, as no connections are possible and no exceptions are thrown.
根据 http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx,这可能是因为必须指定服务器证书(您可以使用 HttpCfg.exe 配置服务器证书和其他侦听器选项").
According to http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx, this might be because one has to specify a server certificate ("You can configure Server Certificates and other listener options by using HttpCfg.exe").
我如何在 MonoTouch 中做到这一点?
How can I do it in MonoTouch?
推荐答案
这是一个很好的问题.在某些情况下,例如对于 HttpListener
,.NET 需要工具或 .config 文件(使用 System.Configuration
)来调整应用程序的配置.在许多情况下,API 确实可以达到相同的目的,但并非总是如此(在这种情况下也不是).
This is a very good question. In some cases, like for HttpListener
, .NET requires tools or .config files (using System.Configuration
) to tweak the configuration of an application. In many cases there are API do achieve the same purpose, but not always (and not in this case).
解决方案是查看 Mono 的源代码,看看它期望 HttpCfg.exe
工具为应用程序设置什么.来自 github:
The solution is to look at Mono's source code to see what it expects the HttpCfg.exe
tool to setup for the application. From 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;
所以解决方案是创建相同的目录结构(这是可能的,因为它会指向Documents
目录下)并复制.cer
文件(二进制DER编码证书)和 .pvk
文件(这是 makecert
创建的格式的私钥),以端口号作为文件名.
So the solution is to create the same directory structure (it's possible since it will point under the Documents
directory) and copy the .cer
file (binary DER-encoded certificate) and the .pvk
file (which is the private key in the format that makecert
creates) with the port number as the file name.
有了这些文件,您应该能够启动 HttpListener
并让它加载处理 SSL 请求所需的证书和私钥.
With those files in place you should be able to start the HttpListener
and have it load the required certificate and private key required to handle SSL requests.
这篇关于在 MonoTouch 上使用 HTTPS 的 HttpListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!