使用DSAPI实现一个简单的WebAPI功能,以便各客户端访问。支持身份验证,支持基础防护。

新建项目(以下演示控制台示例),引用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

  

  

  调试运行效果如下

DSAPI 简单WebAPI实现-LMLPHP

DSAPI 简单WebAPI实现-LMLPHP

传入一个或多个参数

DSAPI 简单WebAPI实现-LMLPHP

DSAPI 简单WebAPI实现-LMLPHP

传入带值的参数

DSAPI 简单WebAPI实现-LMLPHP

DSAPI 简单WebAPI实现-LMLPHP

05-02 06:35