Reputation: 31
I'm trying to capture the webhooks from the site https://www.unbounce.com in my asp.net web forms application.
I've created HttpAsyncHandler in WebHookHandler.cs
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);
}
}
and register my handler and add the map in web.config
<add verb="*" path="webhook.handler" name="WebHookAsyncHandler" type="WebHookHandler"/>
This all is actually taken from msdn (no reputation, sorry)
Next, the other site (unbounce.com) POST something like this:
data.json: {"time_submitted":["04:59 PM UTC"],"page_uuid":["3282389-f13a-44b0-9a49-6321b515d43"],"email":["[email protected]"],"page_name":["Test name"],"date_submitted":["2017-07-17"],"name":["tester"],"ip_address":["80.80.80.80"],"page_url":["http://somepage.url"],"variant":["a"]}
everytime the user presses the button. The POST url is: example.com/webhook.handler
But I don't get the posted data. The output is:
Begin IsThreadPoolThread is True
Completion IsThreadPoolThread is True
Hello World from Async Handler!
I tried also use _context.Request and _context.Request.Form before StreamReader, but they was NULL everytime.
I think, I have some global misunderstading of how these things work. Can you please help me to display the data from the POST request to my site on the page?
Upvotes: 2
Views: 1086
Reputation: 31
Well, it turned out, that everything was ok in my listing. You just need to set up a good testing enviroment to catch these POSTs, or better, to do them yourself.
Upvotes: 1