none
WebServer (HttpRequester) nur lokal

    Frage

  • Hoi,

    ich benutze folgenden Code:

    ' Http Request Handler
    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Threading
    Imports System.Net
    Imports System.Net.HttpListenerContext
    Public Class HttpRequestWorker
    
    
        Public context As HttpListenerContext
        Public caller As GMS
        Public Const basePath As String = "GMS"
        Public Const Port As Integer = 8181
        Public Const baseDirectory As String = "\EGLTD\GMServer\"
        Public Shared validExtensions As String() = {".HTM", ".HTML", ".SYS"}
        Public listener As HttpListener
        Public mainThread As Thread
        Public Sub New(ByVal context As HttpListenerContext, ByVal f As GMS)
            Me.context = context
            caller = f
        End Sub
    
        ' Handle the request
        Public Sub ProcessRequest()
    
            Dim msg As String = context.Request.HttpMethod & " " & _
              context.Request.Url.ToString()
    
            caller.UpdateListBox(msg)
    
            Dim url As System.Uri = context.Request.Url
    
            Dim path As String = url.GetComponents(UriComponents.Path, _
              UriFormat.SafeUnescaped)
            Debug.WriteLine(path)
    
            ' Dim query As String = url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped)
            ' Debug.WriteLine(query)
    
            Dim parts(-1) As String
            Dim file As String = String.Empty
            Dim ext As String = String.Empty
            Dim response As HttpListenerResponse = context.Response
    
            Try
                Dim requestError As Boolean
    
                requestError = context.Request.HttpMethod.ToUpper() <> "GET" _
                  OrElse String.IsNullOrEmpty(path)
    
                If Not requestError Then
                    parts = path.Split("/")
                    requestError = parts.Count <> 2 OrElse parts(0) <> basePath
                End If
    
                If Not requestError Then
                    file = IO.Path.Combine(baseDirectory, parts(1))
                    ext = IO.Path.GetExtension(parts(1).ToUpper())
                    requestError = Not validExtensions.Contains(ext) OrElse Not IO.File.Exists(file)
                End If
    
                If Not requestError Then
                    response.AddHeader("Cache-Control", "no-cache")
                    response.AddHeader("Pragma", "no-cache")
                    response.StatusCode = 200
    
                    Dim encoding As System.Text.Encoding = System.Text.Encoding.Default
                    response.ContentEncoding = encoding
                    response.ContentType = "text/html"
    
                    Dim responseHtml As String = IO.File.ReadAllText(file)
                    Dim responseHtmlBytes() As Byte = encoding.GetBytes(responseHtml)
                    response.ContentLength64 = responseHtmlBytes.Length
    
                    Dim stream As IO.Stream = response.OutputStream
                    stream.Write(responseHtmlBytes, 0, responseHtmlBytes.Length)
                    stream.Close()
                Else
                    response.StatusCode = 404
                End If
            Catch ex As Exception
                response.StatusCode = 500
            Finally
                response.Close()
            End Try
        End Sub
    End Class


    Beim Aufruf im WebBrowser bekomme ich bei http://localhost:9090/GMS/datei.htm die generierte HTML-Seite angezeigt.

    Wenn ich http://192.168.1.77:9090/GMS/datei.htm auf einem anderen PC oder auf dem selben eingebe, bekomme ich eine Fehlermeldung : unbekannter Host

    Alle Rechner sind im lokalen Netz aber ansonsten erreichbar...wenn ich einen FreeWare starte (MiniWebserver) funktioniert alles tadellos (allerdings Port 81)

    Hier Konfig und Aufruf aus dem Programm:

     Public Sub mainRequestLoop()
            listener = New HttpListener()
    
            ' Use Http://hostName:9090/HttpServerRequest/datei.htm
    
            listener.Prefixes.Add("http://localhost:" & Port.ToString() & "/" & basePath & "/")
            listener.Start()
            Try
                Do
                    Dim ctx As HttpListenerContext = listener.GetContext()
                    Dim worker As New HttpRequestWorker(ctx, Me)
                    ' ToDo: use threadpool threads probably better
                    Dim t As New Thread(AddressOf worker.ProcessRequest)
                    t.Start()
                Loop
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End Sub

    Hier der Teil wenn Daten empfangen wurden:

       Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            If e.EventType = IO.Ports.SerialData.Chars Then ShowReceived()
    
        End Sub
    Warum funktioniert das nur lokal und nicht über mein WLan ?


    Be a good forum member - mark posts that contain the answers to your questions or those that are helpful


    • Bearbeitet Trixi-N Montag, 23. September 2013 10:40
    Mittwoch, 22. Mai 2013 16:27