Wednesday, 29 February 2012

C# HttpListener

Setting up your own HttpListener can be useful, particularly when running a Windows service or perhaps unit testing your RESTful services.

Here is a little console app project that serves to remind how to setup the HttpListener and also how to interact with the WebRequest and WebResponse objects.

HttpListenerExample

Source code:  http://stevenhollidge.com/blog-source-code/HttpListenerExample.zip

6 comments:

  1. Nice! Already had a Windows Service providing all the functionality we required. Wanted to serve file attachments from the service over http and didn't want to involve an IIS component. This fit our requirements nicely! Thanks!

    ReplyDelete
  2. I'm just getting "Unable to connect to the remote server" every time I try to run it.

    ReplyDelete
  3. Getting "Unable to connect to the remote server" . I have firewall open I am using port 8080 .

    ReplyDelete
  4. Great sample, thanks

    ReplyDelete
  5. Chinwendu Ochonma13 August 2014 at 12:15

    I tried testing, but when it tries to do listener.start is throws an error of access is denied, kindly assist

    ReplyDelete
    Replies
    1. Make sure u run only on localhost (and not 127.0.0.1) OR
      run as Admin OR
      Allow the URL with netsh

      Delete