Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpListener.GetContext Method

Waits for an incoming request and returns when one is received.

Namespace:  System.Net
Assembly:  System (in System.dll)

'Declaration
Public Function GetContext As HttpListenerContext

Return Value

Type: System.Net.HttpListenerContext
An HttpListenerContext object that represents a client request.

ExceptionCondition
HttpListenerException

A Win32 function call failed. Check the exception's ErrorCode property to determine the cause of the exception.

InvalidOperationException

This object has not been started or is currently stopped.

-or-

The HttpListener does not have any Uniform Resource Identifier (URI) prefixes to respond to. See Remarks.

ObjectDisposedException

This object is closed.

Before calling this method, you must call the Start method and add at least one URI prefix to listen for by adding the URI strings to the HttpListenerPrefixCollection returned by the Prefixes property. For a detailed description of prefixes, see the HttpListener class overview.

This method blocks while waiting for an incoming request. If you want incoming requests to be processed asynchronously (on separate threads) so that your application does not block, use the BeginGetContext method.

Notes to Callers

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following code example demonstrates calling this method.


' This example requires the System and System.Net namespaces.
Public Shared Sub SimpleListenerExample(ByVal prefixes() As String)
    If Not HttpListener.IsSupported Then
        Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.")
        Return
    End If
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    If prefixes Is Nothing OrElse prefixes.Length = 0 Then
        Throw New ArgumentException("prefixes")
    End If

    ' Create a listener.
    Dim listener As New HttpListener()
    ' Add the prefixes.
    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next s
    listener.Start()
    Console.WriteLine("Listening...")
    ' Note: The GetContext method blocks while waiting for a request. 
    Dim context As HttpListenerContext = listener.GetContext()
    Dim request As HttpListenerRequest = context.Request
    ' Obtain a response object.
    Dim response As HttpListenerResponse = context.Response
    ' Construct a response.
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer() As Byte = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    ' You must close the output stream.
    output.Close()
    listener.Stop()
End Sub


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft