我怎样才能实时收听asp.net webforms中的webhooks?

时间:2017-07-17 18:02:00

标签: c# asp.net http post webhooks

我试图在我的asp.net网络表单应用程序中从网站https://www.unbounce.com捕获webhook。

我在 WebHookHandler.cs

中创建了HttpAsyncHandler
public class WebHookHandler:IHttpAsyncHandler
{
    public WebHookHandler()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    public bool IsReusable { get { return false; } }
    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");
        AsynchOperation asynch = new AsynchOperation(cb, context, extraData);
        asynch.StartAsyncWork();
        return asynch;
    }

    public void EndProcessRequest(IAsyncResult result)
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        throw new InvalidOperationException();
    }
}
class AsynchOperation : IAsyncResult
{
    private bool _completed;
    private object _state;
    private AsyncCallback _callback;
    private HttpContext _context;

    bool IAsyncResult.IsCompleted { get { return _completed; } }
    WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }
    object IAsyncResult.AsyncState { get { return _state; } }
    bool IAsyncResult.CompletedSynchronously { get { return false; } }

    public AsynchOperation(AsyncCallback callback, HttpContext context, object state)
    {
        _callback = callback;
        _context = context;
        _state = state;
        _completed = false;
    }

    public void StartAsyncWork()
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
    }

    private void StartAsyncTask(object workItemState)
    {
        _context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");
        _context.Response.Write("Hello World from Async Handler!\r\n");
        using (var reader = new StreamReader(_context.Request.InputStream))
        {    
            string postData = reader.ReadToEnd();
            _context.Response.Write(postData);
        }


        _completed = true;
        _callback(this);
    }
}

并注册我的处理程序并在 web.config

中添加地图
<add verb="*" path="webhook.handler" name="WebHookAsyncHandler" type="WebHookHandler"/>

这一切实际上取自msdn(没有名声,对不起)

接下来,另一个网站(unbounce.com)POST这样的事情:

data.json: {"time_submitted":["04:59 PM UTC"],"page_uuid":["3282389-f13a-44b0-9a49-6321b515d43"],"email":["test@test.com"],"page_name":["Test name"],"date_submitted":["2017-07-17"],"name":["tester"],"ip_address":["80.80.80.80"],"page_url":["http://somepage.url"],"variant":["a"]}

每次用户按下按钮。 POST网址为:example.com/webhook.handler

但我没有获得发布的数据。输出是:

Begin IsThreadPoolThread is True
Completion IsThreadPoolThread is True
Hello World from Async Handler! 

我尝试在StreamReader之前使用_context.Request和_context.Request.Form,但每次都是NULL。

我认为,我对这些事情的运作方式有一些全球性的误解。你能帮我在页面上显示POST请求到我网站的数据吗?

1 个答案:

答案 0 :(得分:1)

原来,我列表中的所有内容都还不错。 您只需要设置一个良好的测试环境即可捕获这些POST,或者更好地自己完成。