Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

When do you get the exception?
Have you tried right clicking and choosing "Run as Administrator"?
Avatar of tonelm54
tonelm54

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial