Webserver - Es kommt nix zurück

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von gfcwfzkm.

    Webserver - Es kommt nix zurück

    Hi,

    ich habe auf vbarchiv.net einen Beispielcode gefunden, jedoch will dieser nicht so richtig.
    Egal, wie oft ich es versuche, es wird beim browser nix angezeigt.
    Hier mal den Code:

    VB.NET-Quellcode

    1. Imports System.Threading
    2. Imports System.Net
    3. Public Class MainForm
    4. Private Const basePath As String = "HttpServerRequest"
    5. Private Const Port As Integer = 80
    6. Private Const baseDirectory As String = "C:\SERVER\Test"
    7. Private Shared validExtensions As String() = {".HTM", ".HTML"}
    8. Private listener As HttpListener
    9. Private mainThread As Thread
    10. Protected Overrides Sub OnFormClosing(ByVal e As _
    11. System.Windows.Forms.FormClosingEventArgs)
    12. ' ToDo: ensure all worker threads are ended
    13. listener.Abort()
    14. mainThread.Join()
    15. End Sub
    16. Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    17. mainThread = New Thread(AddressOf mainRequestLoop)
    18. mainThread.Start()
    19. End Sub
    20. Private Delegate Sub updateListBoxHandler(ByVal msg As String)
    21. Public Sub UpdateListBox(ByVal msg As String)
    22. If InvokeRequired Then
    23. Invoke(New updateListBoxHandler(AddressOf UpdateListBox), New String() {msg})
    24. Else
    25. ListBox1.Items.Add(msg)
    26. End If
    27. End Sub
    28. Private Sub mainRequestLoop()
    29. listener = New HttpListener()
    30. ' Use Http://hostName:9090/HttpServerRequest/datei.htm
    31. listener.Prefixes.Add("Http://*:" & Port.ToString() & "/" & basePath & "/")
    32. listener.Start()
    33. Try
    34. Do
    35. Dim ctx As HttpListenerContext = listener.GetContext()
    36. Dim worker As New HttpRequestWorker(ctx, Me)
    37. ' ToDo: use threadpool threads probably better
    38. Dim t As New Thread(AddressOf worker.ProcessRequest)
    39. t.Start()
    40. Loop
    41. Catch ex As Exception
    42. MsgBox(ex.ToString())
    43. End Try
    44. End Sub
    45. Private Class HttpRequestWorker
    46. Private context As HttpListenerContext
    47. Private caller As MainForm
    48. Public Sub New(ByVal context As HttpListenerContext, ByVal f As MainForm)
    49. Me.context = context
    50. caller = f
    51. End Sub
    52. ' Handle the request
    53. Public Sub ProcessRequest()
    54. Dim msg As String = context.Request.HttpMethod & " " & _
    55. context.Request.Url.ToString()
    56. caller.UpdateListBox(msg)
    57. Dim url As System.Uri = context.Request.Url
    58. Dim path As String = url.GetComponents(UriComponents.Path, _
    59. UriFormat.SafeUnescaped)
    60. Debug.WriteLine(path)
    61. ' Dim query As String = url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped)
    62. ' Debug.WriteLine(query)
    63. Dim parts(-1) As String
    64. Dim file As String = String.Empty
    65. Dim ext As String = String.Empty
    66. Dim response As HttpListenerResponse = context.Response
    67. Try
    68. Dim requestError As Boolean
    69. requestError = context.Request.HttpMethod.ToUpper() <> "GET" _
    70. OrElse String.IsNullOrEmpty(path)
    71. If Not requestError Then
    72. parts = path.Split("/")
    73. requestError = parts.Count <> 2 OrElse parts(0) <> basePath
    74. End If
    75. If Not requestError Then
    76. file = IO.Path.Combine(baseDirectory, parts(1))
    77. ext = IO.Path.GetExtension(parts(1).ToUpper())
    78. requestError = Not validExtensions.Contains(ext) OrElse Not IO.File.Exists(file)
    79. End If
    80. If Not requestError Then
    81. response.AddHeader("Cache-Control", "no-cache")
    82. response.AddHeader("Pragma", "no-cache")
    83. response.StatusCode = 200
    84. Dim encoding As System.Text.Encoding = System.Text.Encoding.Default
    85. response.ContentEncoding = encoding
    86. response.ContentType = "text/html"
    87. Dim responseHtml As String = IO.File.ReadAllText(file)
    88. Dim responseHtmlBytes() As Byte = encoding.GetBytes(responseHtml)
    89. response.ContentLength64 = responseHtmlBytes.Length
    90. Dim stream As IO.Stream = response.OutputStream
    91. stream.Write(responseHtmlBytes, 0, responseHtmlBytes.Length)
    92. stream.Close()
    93. Else
    94. response.StatusCode = 404
    95. End If
    96. Catch ex As Exception
    97. response.StatusCode = 500
    98. Finally
    99. response.Close()
    100. End Try
    101. End Sub
    102. End Class
    103. Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
    104. Me.Close()
    105. End Sub
    106. End Class


    Es kommt kein Fehler unter VB.
    nur kann z.B. IE die Datei nicht finden.

    Bitte um hilfe...

    mfg
    Ja? Hab ich ihn etwa falsch getestet?

    hab Browser geöffnet, localhost/HttpServerRequest/datei.htm
    Es kommt: Nix


    Kapiers nicht. Hast du's anders gemacht?

    Edit:// Hier meine .htm Datei:

    HTML-Quellcode

    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//EN">
    2. <html>
    3. <head>
    4. Hallo
    5. </head>
    6. <body>
    7. Hallo Welt
    8. </body>
    9. </html>
    hmm... kapiers einfach nicht.

    Auch beim schliessen kommt einen Fehler:
    Edit: Hier die Meldung vom IE

    EditEdit:::/// Lol, ich kann nicht local testen, nur von einem anderen Browser aus, also nicht local ^^

    Server funzt, entschuldigung für den unnötigen thread :S
    Bilder
    • Unbenannt.PNG

      36,03 kB, 489×228, 132 mal angesehen
    • Unbenannt1.PNG

      25,42 kB, 758×327, 92 mal angesehen

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „gfcwfzkm“ ()