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

如何在ASP.NET中接收Mollie的x-www-form-urlencoded格式POST Webhook

解决ASP.NET WCF接收Mollie x-www-form-urlencoded Webhook的问题

核心修改步骤

1. 更新服务接口(iTest.vb)

将原OperationContract改为接收Stream类型参数,适配原始请求体:

<ServiceContract()>
Public Interface iTest
    <OperationContract()>
    <WebInvoke(Method:="POST", UriTemplate:="/webhook", BodyStyle:=WebMessageBodyStyle.Bare)>
    Function ReceiveMollieWebhook(requestStream As Stream) As String
End Interface

2. 实现表单数据解析(tst.svc.vb)

在服务实现类中读取并解析x-www-form-urlencoded格式的请求数据:

Imports System.IO
Imports System.Web

Public Class tTest
    Implements iTest

    Public Function ReceiveMollieWebhook(requestStream As Stream) As String Implements iTest.ReceiveMollieWebhook
        Using reader As New StreamReader(requestStream)
            ' 读取完整请求体
            Dim formRawData = reader.ReadToEnd()
            ' 解析为键值对集合
            Dim formData = HttpUtility.ParseQueryString(formRawData)

            ' 提取Mollie Webhook核心参数(示例)
            Dim paymentId = formData("id")
            Dim eventType = formData("eventType")

            ' ********** 关键:验证Mollie签名 **********
            ' 从请求头获取X-Mollie-Signature值,用你的API密钥验证签名合法性
            ' 按Mollie规范实现签名验证,防止伪造请求

            ' 处理业务逻辑...

            Return "OK"
        End Using
    End Function
End Class

3. 修正web.config的WCF配置

确保启用WebHttp绑定,支持HTTP POST请求:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="WebEndpointBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service name="YourNamespace.tTest">
            <endpoint address="" 
                      binding="webHttpBinding" 
                      contract="YourNamespace.iTest" 
                      behaviorConfiguration="WebEndpointBehavior"/>
        </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

额外注意事项

  • 超时问题排查:本地测试时确保服务正常启动,Postman请求URL正确(如http://localhost:端口号/tst.svc/webhook);公网测试需用内网穿透工具暴露本地服务,或直接部署到公网服务器。
  • 格式校验:可在代码中额外校验请求头的Content-Type是否为application/x-www-form-urlencoded,拒绝非法格式请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 12:05:42