You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用VB.NET从Webhook的POST请求URL中获取JSON数据?

用VB.NET从Webhook.site获取请求数据的实现方案

能否从Webhook.site获取数据?

可以。Webhook.site提供官方API查询已收到的请求数据,你只需用到Webhook URL中的令牌(即URL里的随机字符串,比如https://webhook.site/abc123中的abc123)即可调用API。

是否需要安装额外包?

  • 若使用.NET Core/.NET 5+自带的System.Text.Json处理JSON,无需额外安装包;
  • 若选择更易用的Newtonsoft.Json(Json.NET),则需要在NuGet包管理器中搜索并安装Newtonsoft.Json包。

VB.NET实现代码示例

方案1:使用内置System.Text.Json(无需额外包)

Imports System.Net.Http
Imports System.Text.Json

Module WebhookDataFetcher
    Async Sub Main()
        ' 替换为你的Webhook令牌
        Dim webhookToken As String = "你的Webhook令牌"
        ' API地址:获取该令牌下的最新一条请求
        Dim apiUrl As String = $"https://webhook.site/token/{webhookToken}/requests?sort=created_at&order=desc&limit=1"

        Using httpClient As New HttpClient()
            Try
                Dim response As HttpResponseMessage = Await httpClient.GetAsync(apiUrl)
                response.EnsureSuccessStatusCode() ' 校验请求是否成功

                Dim jsonString As String = Await response.Content.ReadAsStringAsync()
                ' 反序列化API返回的请求列表
                Dim requests = JsonSerializer.Deserialize(Of List(Of WebhookRequest))(jsonString)

                If requests?.Count > 0 Then
                    ' 获取最新请求的JSON body
                    Dim latestRequestBody = requests(0).Request.Body
                    Console.WriteLine("最新请求的JSON数据:")
                    Console.WriteLine(latestRequestBody)

                    ' 解析body中的员工列表
                    Dim employeeData = JsonSerializer.Deserialize(Of EmployeeData)(latestRequestBody)
                    Console.WriteLine(vbCrLf & "解析后的员工列表:")
                    For Each emp In employeeData.Employees
                        Console.WriteLine($" - {emp}")
                    Next
                Else
                    Console.WriteLine("暂无收到任何请求数据")
                End If
            Catch ex As Exception
                Console.WriteLine($"请求出错:{ex.Message}")
            End Try
        End Using
    End Sub

    ' 对应Webhook.site API返回的结构
    Public Class WebhookRequest
        Public Property Request As RequestDetails
    End Class

    Public Class RequestDetails
        Public Property Body As String
    End Class

    ' 对应你收到的JSON数据结构
    Public Class EmployeeData
        Public Property Employees As List(Of String)
    End Class
End Module

方案2:使用Newtonsoft.Json(需安装包)

Imports System.Net.Http
Imports Newtonsoft.Json

Module WebhookDataFetcher
    Async Sub Main()
        Dim webhookToken As String = "你的Webhook令牌"
        Dim apiUrl As String = $"https://webhook.site/token/{webhookToken}/requests?sort=created_at&order=desc&limit=1"

        Using httpClient As New HttpClient()
            Try
                Dim response As HttpResponseMessage = Await httpClient.GetAsync(apiUrl)
                response.EnsureSuccessStatusCode()

                Dim jsonString As String = Await response.Content.ReadAsStringAsync()
                Dim requests = JsonConvert.DeserializeObject(Of List(Of WebhookRequest))(jsonString)

                If requests?.Count > 0 Then
                    Dim latestRequestBody = requests(0).Request.Body
                    Console.WriteLine("最新请求的JSON数据:")
                    Console.WriteLine(latestRequestBody)

                    Dim employeeData = JsonConvert.DeserializeObject(Of EmployeeData)(latestRequestBody)
                    Console.WriteLine(vbCrLf & "解析后的员工列表:")
                    For Each emp In employeeData.Employees
                        Console.WriteLine($" - {emp}")
                    Next
                Else
                    Console.WriteLine("暂无收到任何请求数据")
                End If
            Catch ex As Exception
                Console.WriteLine($"请求出错:{ex.Message}")
            End Try
        End Using
    End Sub

    Public Class WebhookRequest
        Public Property Request As RequestDetails
    End Class

    Public Class RequestDetails
        Public Property Body As String
    End Class

    Public Class EmployeeData
        Public Property Employees As List(Of String)
    End Class
End Module

注意事项

  • 保护好你的Webhook令牌,避免泄露给他人,否则对方可以查看所有发送到该Webhook的请求数据;
  • API URL中的limit=1参数用于只获取最新的一条请求,若需要更多数据可调整该值或移除参数;
  • 若请求失败,可检查网络连接、令牌是否正确,或核对Webhook.site的API接口规则。

内容的提问来源于stack exchange,提问作者Duong Sansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:07:14