Home
C# Web Listener Absolute Basics
Starter for 10 getting C# .NetCore 6 to listen for a HTTP request...
You WILL need to run Visual Studio as Administrator otherwise the code can't get permission to grab the various windows code to actually run the listener.
using System.Net;
using System.Text;
HttpListener httpListener = new HttpListener();
httpListener.Prefixes.Add("http://*:8080/");
httpListener.Start();
Console.WriteLine("Started");
while (true)
{
HttpListenerContext httpListenerContext = httpListener.GetContext();
using (HttpListenerResponse httpListenerResponse = httpListenerContext.Response)
{
httpListenerResponse.Headers.Set("Content-Type", "text/plain");
string data = "Hello World";
byte[] buffer = Encoding.UTF8.GetBytes(data);
httpListenerResponse.ContentLength64 = buffer.Length;
using Stream ros = httpListenerResponse.OutputStream;
ros.Write(buffer, 0, buffer.Length);
}
}
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home