2011-12-28 4 views
1

아래 코드는 listener.GetContext()에서 멋지게 기다리는 HTTPLISTENER를 생성합니다.내가 만든 HTTPLISTENER와 어떻게 통신합니까?

다른 VB 앱과 어떻게 통신합니까? WebRequest를 얻을 수없는 것 같습니다. 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/http-listener에 /"당신의 HTTP 수신기에 연결할 수

는 그런 다음 URL을 다음 호출 할 필요가 :

" http://127.0.0.1:8080/HttpListener/ " - 또는 - 에"http : // localhost를 : 8080/http-listener에/"나는이 도움이 되었으면 좋겠

0

:-) 가장 쉬운 방법 HttpListener가 실제로 듣고 있는지 확인하는 것입니다. 브라우저로 듣고있는 URL로 이동합니다. 찾지 못하면 404 오류가 발생합니다.

수신기가 작동하는지 확인했으면 WebClient를 사용하여 코드에서 통신하십시오. WebClient는 HttpWebRequest보다 훨씬 간단한 인터페이스를 제공하며 스트림에서 읽고 쓰는 일을 처리합니다.

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