Results 1 to 2 of 2

Thread: How do I communicate with an HTTPLISTENER that I have created?

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    How do I communicate with an HTTPLISTENER that I have created?

    I've got this code below that creates an HTTPLISTENER that is nicely waiting at listener.GetContext().

    Code:
    Imports System.Net
    Imports System.Globalization
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim prefixes() As String = {"http://*:8080/HttpListener/"}
    
            ProcessRequests(prefixes)
    
        End Sub
    
        Private Sub ProcessRequests(ByVal prefixes() As String)
            If Not System.Net.HttpListener.IsSupported Then
                Console.WriteLine( _
                    "Windows XP SP2, Server 2003, or higher is required to " & _
                    "use the HttpListener class.")
                Exit Sub
            End If
    
            ' URI prefixes are required,
            If prefixes Is Nothing OrElse prefixes.Length = 0 Then
                Throw New ArgumentException("prefixes")
            End If
    
            ' Create a listener and add the prefixes.
            Dim listener As System.Net.HttpListener = _
                New System.Net.HttpListener()
            For Each s As String In prefixes
                listener.Prefixes.Add(s)
            Next
    
            Try
                ' Start the listener to begin listening for requests.
                listener.Start()
                Console.WriteLine("Listening...")
    
                ' Set the number of requests this application will handle.
                Dim numRequestsToBeHandled As Integer = 10
    
                For i As Integer = 0 To numRequestsToBeHandled
                    Dim response As HttpListenerResponse = Nothing
                    Try
                        ' Note: GetContext blocks while waiting for a request. 
                        Dim context As HttpListenerContext = listener.GetContext()
    
                        ' Create the response.
                        response = context.Response
                        Dim responseString As String = _
                            "<HTML><BODY>The time is currently " & _
                            DateTime.Now.ToString( _
                            DateTimeFormatInfo.CurrentInfo) & _
                            "</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
                Next
            Catch ex As HttpListenerException
                Console.WriteLine(ex.Message)
            Finally
                ' Stop listening for requests.
                listener.Close()
                Console.WriteLine("Done Listening...")
            End Try
        End Sub
    
    End Class
    How do I communicate with this from another VB app? I cannot seem to get WebRequest.Create to work with the URI that my HTTPLISTENER example is using.

    This line of code from a second app is not working -
    Code:
    Dim prefixes() As String = {"http://*:8080/HttpListener/"}
    Dim request As WebRequest = WebRequest.Create(prefixes(0))

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How do I communicate with an HTTPLISTENER that I have created?

    I got it - had to change the */ to localhost/ - not sure what that is all about

    What are the rules for URI naming and such???

    At any rate - here is an attachment showing it running in VS and also FF

    So cool - I can now talk to my running background services with a browser or another app!
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width