Reputation: 107
I am trying to create a simple https server in c# in a Unity3D game, to be accessed through a web browser. I've created a server cert and key with openssl, but I can't find a multi-platform way to pass the cert to the server without any additional configuration outside the code.
Most of the info I've been able to find falls into these categories:
I know in python you do something like:
ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)
...but there doesn't seem to be an equivalent in c# for httplistener, or in system.security.securitymanager or anything. I assume/hope that I'm just missing something obvious here.
For what it's worth, here's what I have so far, which is just the MSDN httplistener example put in a Unity script:
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 () {
}
}
Upvotes: 4
Views: 5785
Reputation: 3477
If you came here from Google trying to find information on HttpListener in Mono and SSL, you will find more pertinent information on this related question:
Mono HttpListener client certificate
The OPs originally desire is a simple web server without any platform-specific configuration. So far, the only library I've found that supports an approach that avoids platform configuration is Ceen HTTPd.
There was a discussion about Ceen from a poster with a similar need here: https://softwarerecs.stackexchange.com/questions/52304/net-library-for-running-an-embedded-selfhosted-light-web-server-from-c-with-ss
Upvotes: 1