Created
November 14, 2017 20:35
-
-
Save pingec/9bf2fd92ac85032b5e30d487a35789a0 to your computer and use it in GitHub Desktop.
C# Simple http server to dump requests to console (HttpListener)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
namespace DumpHttpRequests | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
if (!HttpListener.IsSupported) | |
{ | |
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); | |
return; | |
} | |
// URI prefixes are required, | |
var prefixes = new List<string>() { "http://*:8888/" }; | |
// Create a listener. | |
HttpListener listener = new HttpListener(); | |
// Add the prefixes. | |
foreach (string s in prefixes) | |
{ | |
listener.Prefixes.Add(s); | |
} | |
listener.Start(); | |
Console.WriteLine("Listening..."); | |
while (true) | |
{ | |
// Note: The GetContext method blocks while waiting for a request. | |
HttpListenerContext context = listener.GetContext(); | |
HttpListenerRequest request = context.Request; | |
string documentContents; | |
using (Stream receiveStream = request.InputStream) | |
{ | |
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) | |
{ | |
documentContents = readStream.ReadToEnd(); | |
} | |
} | |
Console.WriteLine($"Recived request for {request.Url}"); | |
Console.WriteLine(documentContents); | |
// 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment