簡體   English   中英

通過 ASP.NET 接受簡單的 x-www-form-urlencoded POST webhook 調用

[英]Accepting simple x-www-form-urlencoded POST webhook call via ASP.NET

我想通過 Mollie 的 ASP.NET 接受一個簡單的 x-www-form-urlencoded POST webhook 調用。 所以基本上對方是在調用我的webhook。

這是有問題的 webhook: https://docs.mollie.com/overview/webhooks

我已經在這里檢查過:

但無論我嘗試什么,當我檢查 Postman 時,我都會遇到連接超時:

在此處輸入圖像描述

這是我的代碼:

iTest.vb

Imports System.ServiceModel
Imports System.Web
Imports System.IO
Imports System.Runtime.Remoting.Activation
Imports System.Collections.Generic
Imports System.ServiceModel.Web

<ServiceContract()>
Public Interface Itest

    <OperationContract()>
    <Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.WrappedRequest,
UriTemplate:="webhook_mollie")>
    Function Webhook(ByVal whook As GlobalFunctions.webhookData) As Boolean
    
End Interface

tst.svc.vb

Public Class tst
    Implements Itest

    Public Function Webhook(ByVal whook As GlobalFunctions.webhookData) As Boolean Implements Itest.Webhook
         Return True
    End Function

End Class


Public Class webhookData

    Private _id As String
    <Runtime.Serialization.DataMember>
    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property
End Class

由於 web.config 中的重寫規則,postman 請求中的“/api”部分適用於我的其他 API 請求:

    <rule name="RemoveSvcExtension" stopProcessing="true">
      <match url="^(.*)api/(.*)$"/>
      <action type="Rewrite" url="{R:1}tst.svc/{R:2}" logRewrittenUrl="true"/>
    </rule>

我還嘗試了直接 Postman 請求 URL: https://www.example.com/tst.svc/webhook_mollie ,但超時結果相同。

更新 1

好的,我更進一步了。 我現在收到此錯誤:

服務器在處理請求時遇到錯誤。 請參閱服務幫助頁面以構建對服務的有效請求。 異常消息是“傳入消息具有意外的消息格式‘Raw’”。 該操作的預期消息格式為“Xml”; '傑森'。 這可能是因為尚未在綁定上配置 WebContentTypeMapper。 有關詳細信息,請參閱 WebContentTypeMapper 的文檔。'。

我將定義更改為:

<OperationContract()>
<Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.Bare,UriTemplate:="webhook_mollie")>

但是有同樣的錯誤。 這篇文章似乎最相關,但它有一個完全不同的網絡服務定義格式。 如何重新定義當前的OperationContract以接受Content-Type: application/x-www-form-urlencoded請求?

我通過閱讀 stream 並獲取名稱-值對來獲取 Mollie Webhook 帖子。

public async void Webhook(Stream stream)
{
    var collection = new NameValueCollection();
    string mollieID = "";

    //check the stream
    if (stream == null)
    {
        return;
    }

    //get the posted data from the stream
    try
    {
        using (var reader = new StreamReader(stream))
        {
            collection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(reader.ReadToEnd()));
        }

        mollieID = collection["id"];
    }
    catch (Exception ex)
    {
        //log error
    }

    //check if there is a mollie id
    if (string.IsNullOrEmpty(mollieID))
        return;

    //check the payment status
    await CheckPaymentStatus(mollieID);
}

下面的 VB(使用了轉換器,因此可能不是 100% 准確)

Public Async Sub Webhook(ByVal stream As Stream)
    Dim collection = New NameValueCollection()
    Dim mollieID As String = ""

    If stream Is Nothing Then
        Return
    End If

    Try

        Using reader = New StreamReader(stream)
            collection = HttpUtility.ParseQueryString(HttpUtility.UrlDecode(reader.ReadToEnd()))
        End Using

        mollieID = collection("id")
    Catch ex As Exception
        //log error
    End Try

    If String.IsNullOrEmpty(mollieID) Then Return
    Await CheckPaymentStatus(mollieID)
End Sub

並在界面中默認RequestFormatBodyStyle

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "mollie")]
void Webhook(Stream stream);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM