DSAPI 簡單WebAPI實現

使用DSAPI實現一個簡單的WebAPI功能,以便各客戶端訪問。支持身份驗證,支持基礎防禦。android

新建項目(如下演示控制檯示例),引用DSAPI.dll。瀏覽器

複製粘貼如下代碼:網絡

Module Module1
    Private WithEvents SERVER As New DSAPI.網絡.HTTP監聽
    Sub Main()

        With SERVER
            .綁定端口 = 83
            .編碼 = System.Text.Encoding.UTF8
            .容許外網鏈接 = True
            '.參數前導符 = "API"
            '.使用參數前導符 = True
            .傳輸使用Bas64加密 = False
            .啓動監聽()
        End With
        Dim IP As String = DSAPI.網絡.本機內網IP
        Console.WriteLine("手機端訪問時請使用 " & String.Concat("http://", IP, ":", SERVER.綁定端口))
        Console.WriteLine("訪問示例:" & String.Concat("http://", IP, ":", SERVER.綁定端口, "/gettext&getmsg"))

        Console.ReadKey(True)
    End Sub

    Private Sub SERVER_收到被阻止的客戶端請求(客戶端 As DSAPI.網絡.HTTP監聽.監聽客戶端) Handles SERVER.收到被阻止的客戶端請求
        SERVER.寫入信息到指定客戶端輸出流(客戶端, "非法訪問")
    End Sub

    Private Sub SERVER_收到客戶端請求(客戶端 As DSAPI.網絡.HTTP監聽.監聽客戶端) Handles SERVER.收到客戶端請求
             Console.WriteLine(客戶端.傳入的URL)
        '判斷是否有"token"參數而且值爲和本機時間"分鐘"值差異在2之內,此檢測僅爲防止部分自動請求,無關緊要
        'If GetToken(客戶端.傳入的URL) = False Then Exit Sub
        Dim Values As List(Of KeyValue) = GetKeyValues(客戶端.傳入的URL)
        If Values Is Nothing Then Exit Sub
        Dim Lst As New List(Of String)
        For Each Kv As KeyValue In Values
            Select Case Kv.Key.ToLower
                Case "gettext"
                    Lst.Add(String.Concat("Title=", Console.Title))
                Case "getmsg"
                    Lst.Add(String.Concat("Msg=", "如今是測試階段"))
                Case "settitle"
                    Lst.Add("OK")
                    Console.Title = Kv.Value
                Case "setmsg"
                    Lst.Add("OK")
                    Console.WriteLine(Kv.Value)
            End Select
        Next
        Dim Mode As String = "PC端"
        Dim Info As String = 客戶端.瀏覽器信息.ToLower
        If Info.Contains("android") Then
            Mode = "安卓端"
        End If
        Lst.Add(String.Concat("您正在使用", Mode, "請求數據"))
        SERVER.寫入信息到指定客戶端輸出流(客戶端, Join(Lst.ToArray, vbCr))
    End Sub
    Private Function GetKeyValues(URL As String) As List(Of KeyValue)
        Try
            Dim Lst As New List(Of KeyValue)
            Dim S As String = URL.ToLower
            If S.Contains("&") Then
                Dim SP() As String = S.Split("&")
                For Each T As String In SP
                    If T.Contains("=") = False Then
                        Lst.Add(New KeyValue(T, ""))
                    Else
                        Lst.Add(New KeyValue(T.Substring(0, T.IndexOf("=")), T.Substring(T.IndexOf("=") + 1)))
                    End If
                Next
            Else
                If URL.Contains("=") = False Then Return New List(Of KeyValue)({New KeyValue(URL, "")})
                Lst.Add(New KeyValue(URL.Substring(0, URL.IndexOf("=")), URL.Substring(URL.IndexOf("=") + 1)))
            End If
            Return If(Lst.Count = 0, Nothing, Lst)
        Catch
            Return Nothing
        End Try
    End Function
    Private Function GetToken(URL As String) As Boolean
        Try
            Dim S As String = URL.ToLower
            Dim Cmd As String = "token="
            If S.Contains(Cmd) = False Then Return False
            If S.Length < Cmd.Length + 2 Then Return False
            Dim M As String = S.Substring(S.IndexOf(Cmd) + Cmd.Length)
            If IsNumeric(M) = False Then Return False
            Dim N As Integer = M
            Return If(Math.Abs(M - Now.Minute) < 2, True, False)
        Catch
            Return False
        End Try
    End Function
    Friend Class KeyValue
        Public Key As String = ""
        Public Value As String = ""
        Sub New()
        End Sub
        Sub New(_Key As String, _Value As String)
            Key = _Key
            Value = _Value
        End Sub
    End Class
End Module

  

  

  調試運行效果以下測試

 

 

傳入一個或多個參數編碼

 

 

 

傳入帶值的參數加密

相關文章
相關標籤/搜索