System.Net.HttpListenerException - Access denied

I'm trying to develop an application which will be configured by a web interface, so to start I just want to create a simple application which will respond to a web request with a simple 'hello world'.

So to start with, Ive got the following:-
Imports System.Net

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not System.Net.HttpListener.IsSupported Then
            MsgBox("Windows XP SP2, Server 2003, or higher is required to use the HttpListener class.")
            Exit Sub
        End If

        Dim listener As System.Net.HttpListener = New System.Net.HttpListener()

        listener.Prefixes.Add("http://*:8091/HttpListener/")

        listener.Start()
        Dim response As HttpListenerResponse = Nothing
        Try
            Dim context As HttpListenerContext = listener.GetContext()

            response = context.Response
            Dim responseString As String = "<HTML><BODY>Hello</BODY></HTML>"
            Dim buffer() As Byte = System.Text.Encoding.UTF8.GetBytes(responseString)
            response.ContentLength64 = buffer.Length
            Dim output As System.IO.Stream = response.OutputStream
            output.Write(buffer, 0, buffer.Length)

        Catch ex As HttpListenerException
            Console.WriteLine(ex.Message)
        Finally
            If response IsNot Nothing Then
                response.Close()
            End If
        End Try
    End Sub
End Class

Open in new window


However, all Im getting when I try and run it is:-
An unhandled exception of type 'System.Net.HttpListenerException' occurred in System.dll
Additional information: Access is denied

I think its cause Im not running it as an administrator, however Ive configured the manifest to run as:-
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
without success.

Does anyone have any suggestions what I need to do to allow my application to run?

Thank you in advance
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
My first thought would be that the port is in use.

When do you get the exception?
CodeCruiserCommented:
Have you tried right clicking and choosing "Run as Administrator"?
tonelm54Author Commented:
Cause I'm running it through Visual studio I cant run the debug session as administrator can I?

I have tried opening ports in a for loop between 1 and 99999 inside a for loop, and every port says denied
CodeCruiserCommented:
It seems to be a permissions issue. You can go to your project's bin folder and launch the application directly as administrator.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.