【问题标题】:How do I communicate with an HTTPLISTENER that I have created?如何与我创建的 HTTPLISTENER 通信?
【发布时间】:2011-12-28 11:37:08
【问题描述】:

我在下面有这段代码,它创建了一个 HTTPLISTENER,它在 listener.GetContext() 处很好地等待。

如何从另一个 VB 应用程序与此通信?我似乎无法让 WebRequest.Create 与我的 HTTPLISTENER 示例正在使用的 URI 一起工作。第二个应用程序的这行代码不起作用:

Dim request As WebRequest = WebRequest.Create(prefixes(0))

代码如下:

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

【问题讨论】:

    标签: vb.net httplistener


    【解决方案1】:

    您不能按原样使用前缀!您可能需要将“*”替换为“127.0.0.1”才能连接到您的监听器。所以如果你的前缀是这样的:

    “http://*:8080/HttpListener/”

    然后您需要调用以下 URL 才能连接到您的 HTTP 侦听器:

    "http://127.0.0.1:8080/HttpListener/" - 或者 - "http://localhost:8080/HttpListener/"

    我希望这会有所帮助:-)

    【讨论】:

      【解决方案2】:

      验证您的 HttpListener 是否确实在侦听的最简单方法是使用浏览器导航到您正在侦听的 url。如果找不到它,您将收到 404 错误。

      一旦您确认侦听器正在工作,然后尝试在您的代码中使用 WebClient 与它进行通信。 WebClient 的接口比 HttpWebRequest 简单得多,并为您处理流的读取和写入。

      string result = WebClient.DownloadString("http://google.com");
      Console.WriteLine(result);
      

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 2013-11-12
        • 2020-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多