VB.net 接收 POST 请求 - 简易 HTTP 服务器

7
我希望有一个程序能够监听特定端口上的帖子,例如:http://xxx.xxx.xxx.xxx:60002?key=value 其中xxx.xxx.xxx.xxx是运行我的程序的IP地址,它正在监听的端口是60002。然后,该程序将需要获取传递给它的参数,例如本例中的keyvalue
我想要能够解析传输的值。 VB不是我通常使用的语言。
我希望这个解决方案与.NET 3.5的框架兼容。
1个回答

10

对下面代码片段进行了轻微修改(来自http://social.msdn.microsoft.com/Forums/vstudio/en-US/b7f476d1-3147-4b18-ba5e-0b3ce8f8a918/want-to-make-a-webserver-with-httplistener),这对我有用:

Imports System.Net
Imports System.Globalization

Module HttpListener

    Sub Main()
        Dim prefixes(0) As String
        prefixes(0) = "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 Module

1
我喜欢它!我必须使用admin-CMD.exe命令“netsh http add urlacl url=http://+:80/ user=NIBTOP\Nibbels”来提升启动此服务器的权限。因此,我使用'prefixes(0) = "http://+:8080/"'。然后在Chrome中启动“http://localhost:8080/”会显示“当前时间为2016年5月13日16:06:16”。 - Nibbels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接