本文介绍了在MonoTouch上使用HTTPS的HttpListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

根据,这可能是因为必须指定服务器证书(您可以配置服务器证书和使用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 工具。来自:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 01:47