3

does anyone know how to get a C# server application running in the console to display HTTP requests from a client as shown in this image?

https://i.stack.imgur.com/vC0k1.jpg

Thanks in advance!

  • Thank you. This is a good answer. It was actually log4net that made that output though but this is almost as good. – user3973478 Aug 25 '14 at 02:52
  • Welcome to [so]. Questions here should __show research effort or attempts__. Please take a __[tour]__. – Unihedron Aug 25 '14 at 05:41

2 Answers2

2

If all you want to do is spin up an HTTP server and write out requests to the console, it should be pretty easy to accomplish using OWIN and Katana. Just install the following NuGet packages:

  • Microsoft.Owin.Hosting
  • Microsoft.Owin.Host.HttpListener

And use something along the lines of the following:

public static class Program
{
    private const string Url = "http://localhost:8080/";

    public static void Main()
    {
        using (WebApp.Start(Url, ConfigureApplication))
        {
            Console.WriteLine("Listening at {0}", Url);
            Console.ReadLine();
        }
    }

    private static void ConfigureApplication(IAppBuilder app)
    {
        app.Use((ctx, next) =>
        {
            Console.WriteLine(
                "Request \"{0}\" from: {1}:{2}",
                ctx.Request.Path,
                ctx.Request.RemoteIpAddress,
                ctx.Request.RemotePort);

            return next();
        });
    }
}

You can of course tweak the output to your liking, having access to the full request and respose objects.

It will give you something like this:

HTTP requests

khellang
  • 17,550
  • 6
  • 64
  • 84
0

You can do this using System.Diagnostics Tracing in Web API. On the asp.net website, you'll be able to read a detailed article.

Another possibility is turning on the IIS logging and then read the logfiles. I'm not quite sure how this is done, it's just what I do on apache2/linux, where you can tail -f log. I read something about powershell equivalents, but not for consoleapps, so I think I would stick with the Tracing.

Edit: After some looking around on SO I found this similar question with relevant answers: How do I see the raw HTTP request that the HttpWebRequest class sends?

Community
  • 1
  • 1
Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37