加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.net接收帖子 – 简单的HTTP服务器

发布时间:2020-12-17 00:32:31 所属栏目:大数据 来源:网络整理
导读:我希望有一个程序可以监听特定端口上的帖子,例如 http://xxx.xxx.xxx.xxx:60002?key=value 其中xxx.xxx.xxx.xxx正在运行我的程序,它正在侦听的端口是60002.程序将需要到达传递给它的参数,在这种情况下键和值 然后我希望能够解析出现的值. VB不是我通常使用的
我希望有一个程序可以监听特定端口上的帖子,例如 http://xxx.xxx.xxx.xxx:60002?key=value

其中xxx.xxx.xxx.xxx正在运行我的程序,它正在侦听的端口是60002.程序将需要到达传递给它的参数,在这种情况下键和值

然后我希望能够解析出现的值. VB不是我通常使用的语言.

我希望该解决方案与.NET 3.5的框架兼容.

稍微修改以下代码片段(从 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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读