2011-12-28 57 views
1

我已經得到了下面的代碼,創建了一個很好地在listener.GetContext()等待的HTTPLISTENER。如何與我創建的HTTPLISTENER通信?

如何從另一個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 

回答

1

不能使用前綴,因爲它是!您可能需要將「*」替換爲「127.0.0.1」才能連接到您的監聽器。所以,如果你的前綴是一樣的東西:

的 「http:// *:8080/HttpListener /」

然後你需要調用下面的URL能夠連接到你的HTTP監聽器:

「 http://127.0.0.1:8080/HttpListener/」 --or - 的 「http://本地主機:8080/HttpListener /」

我希望這有助於:-)

0

最簡單的方法來驗證你的HttpListener實際上是在監聽使用瀏覽器導航到您正在監聽的網址。如果找不到它,你會得到一個404錯誤。

一旦您確認了偵聽器正在工作,然後嘗試使用WebClient在您的代碼中與它進行通信。 WebClient的接口比HttpWebRequest簡單得多,並且負責爲你讀取和寫入數據流。

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