0 Replies - 2897 Views - Last Post: 28 November 2012 - 08:18 AM Rate Topic: -----

#1 ksauerwald  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-November 12

httplistener issue..crashes when attempt is made..

Posted 28 November 2012 - 08:18 AM

ok I have some code I use to get HTTP posts to a URL.. one 7 PC's this works fine ..but on 2 of them this code crashes when it tries to get listen on the URL. (http://LocalHost/SyngoWorkflowBridge/)
I have to assume something is not letting the PC listen on the url.. I never get to this

PrintTrace(0, "Listener Started")

any pointers on debugging would be great... I guess I need to see if there is someway to know
what the other program is that is blocking me or has a hold on it.. or am I doing this wrong
and I should use a port other than the standard 80 ?

here is the code I use
Public Sub AsynchronousListener(ByVal prefixes() As String)
        PrintTrace(1, "AsynchronousListener")
        ' spin up listener
        PrintTrace(0, "Create new Listener object")
        listener = New HttpListener()
        ' add URI prefixes to listen for
        Dim s As String
        For Each s In prefixes
            PrintTrace(0, "Set to Listening on URL-" + s)
        Next s
        PrintTrace(0, "Listener Started")
        ' Create the delegate using the method to update the UI
        Dim _invokeControl As New InvokeControl(AddressOf InvokeUIThread)

        'listBox1.Invoke(_invokeControl, "Entering request processing loop")
        While runServer

            Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf AsynchronousListenerCallback), listener)
            ' intermediate work can go on here while waiting for the asynchronous callback
            ' an asynchronous wait handle is used to prevent this thread from terminating
            ' while waiting for the asynchronous operation to complete.
            'listBox1.Invoke(_invokeControl, "Waiting for asyncronous request processing.")
            'listBox1.Invoke(_invokeControl, "Asynchronous request processed.")

        End While
        ' If the runServer flag gets set to false, stop the server and close the listener.
    End Sub 'AsynchronousListener

    '/ In order to safely update the UI across threads, a delegate with this method is
    '/ called using Control.Invoke
    Private Shared Sub InvokeUIThread(ByVal [text] As String)
    End Sub 'InvokeUIThread

and my Callback code

' Method called back when a client connects.  BeginGetContext contains the AsynchCallback delegate
    ' for this method.
    ' param name result is the state object containing the HttpListener instance
    Public Sub AsynchronousListenerCallback(ByVal result As IAsyncResult)
        PrintTrace(1, "AsynchronousListenerCallback")
        Dim RequestString As String
            Dim listener As HttpListener = CType(result.AsyncState, HttpListener)
            ' Call EndGetContext to signal the completion of the asynchronous operation.
            Dim context As HttpListenerContext = listener.EndGetContext(result)
            'Dim context As HttpListenerContext = listener.GetContext(result)
            Dim request As HttpListenerRequest = context.Request
            Dim body As Stream = request.InputStream
            'Dim encoding1 As System.Text.Encoding = request.ContentEncoding
            'Dim reader1 As New StreamReader(body, encoding1)
            Dim reader1 As New StreamReader(request.InputStream, request.ContentEncoding)
            RequestString = reader1.ReadToEnd
            XMLStringPOST = RequestString
            PrintTrace(0, "XMLStringPOST=" + XMLStringPOST)
            'RequestString = context.Request.ToString()
            ' Get the response object to send our confirmation.
            Dim response As HttpListenerResponse = context.Response
            ' Construct a minimal response string.
            Dim responseString As String = "<HTML><BODY>The time is currently " & _
                 DateTime.Now.ToString( _
                       DateTimeFormatInfo.CurrentInfo) & _
                       RequestString & _
            Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
            ' Get the response OutputStream and write the response to it.
            response.ContentLength64 = buffer.Length
            ' Identify the content type.
            response.ContentType = "text/html"
            Dim output As System.IO.Stream = response.OutputStream
            output.Write(buffer, 0, buffer.Length)
            ' Properly flush and close the output stream
            'Trace.WriteLine("read http request")
            'Call CloseThreadAndProcess()

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Fatal Exception from Listener")
        End Try
    End Sub 'AsynchronousListenerCallback

Is This A Good Question/Topic? 0
  • +

Page 1 of 1