0

I have program that later needed ability to broadcast an webpage, it basically works, however, only way it can be accessed is from the same machine that program is running on, client says and I agree with him that for our use case there might be need for it to be accessible from other devices in the LAN

The code:

using System;
using System.Net;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var web = new HttpListener();
            web.Prefixes.Add("http://localhost:10001/");
            web.Start();
            var context = web.GetContext();
            var response = context.Response;
            const string responseString = "<html><body>Hello world</body></html>";
            var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            var output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();
            web.Stop();
            Console.ReadKey();
        }
    }
}

I can connect with address localhost:10001, but when using my local IP + :10001 I get

400 BAD REQUEST, invalid hostname

in browser both on same computer and other devices

Ok, maybe just change address mentioned in code to local IP + :10001?

using System;
using System.Net;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var web = new HttpListener();
            web.Prefixes.Add("http://192.168.0.12:10001/");
            web.Start();
            var context = web.GetContext();
            var response = context.Response;
            const string responseString = "<html><body>Hello world</body></html>";
            var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            var output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();
            web.Stop();
            Console.ReadKey();
        }
    }
}

No, when I run it an exception is thrown

access denied

in the line web.Start();

I use Windows 10 Pro x64 and latest Firefox, recently updated solutions .NET version to latest

Yes there have been similar questions but they are way too old to trust and they involve using different Windows tools which isn't best solution for plug and play program, fix in code or pointing out what I have done wrong would be perfect, thanks in advance!

  • 1
    Is the firewall enabled on your PC? Also, did you try to start your program in admin mode? Or when debugging, the VS should be started as admin (with the run as administrator). – Reno Apr 15 '20 at 11:00
  • @Reno Running as administrator worked! Yeah I should have thought about that when seeing `access denied` :D Thanks man!! – Dzintars Straupe Apr 15 '20 at 11:16
  • Open `cmd` with elevated rights and execute `netsh http add urlacl url=http://+:10001/ user=Everyone` (or a specific user). Change your socket/prefix to the exact same string, i.e.: "http://+:10001/". That way you do not have to launch your editor with elevated rights. [credits to @Darrel Miller](https://stackoverflow.com/a/4115328/2590375) – nilsK Apr 15 '20 at 11:43

0 Answers0