如何制作"webhook"?
How to make a "webhook"?
我需要 "WinForm" 申请 viber 通讯。
"Webhook" 计划从 viber 接收数据(事件),然后数据将在应用程序 "WinForm" 中使用。
我做到了:
- 创建项目"ASP.NET Web Application (.NET Framework)";
- 选择模板 - "Empty" + "MVC" + "API";
- 添加了控制器"Controller MVC 5 - empty"。控制器名称"HookController";
- 我运行申请"Postman";
- "Postman"。我设置请求"POST";
- "Postman"。我设置了 link
http://localhost:44836/Hook
;
- "Postman"。点击【=113=】;
- 结果,见图"-=RESULT=-";
如果我对理论理解正确,那么在执行"Postman" action. I click "SEND"
之后,ViberProcess (HttpContext context)
方法应该在HookController.cs
控制器中执行,代码应该停止在[=16] =].
这不会发生。
文档 Viber REST API
- link
问题。
如何制作Webhook?
代码HookController.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//
using System.Runtime.Remoting.Contexts;
namespace WebAppl1.Controllers
{
public class HookController : Controller
{
// GET: Hook
//public ActionResult Index()
//{
// return View();
//}
[HttpPost]
// [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void ViberProcess(HttpContext context)
{
try
{
Stream s = context.Request.InputStream;
// Stream s = Context.Request.InputStream;
// or Stream s = HttpContext.Current.Request.InputStream;
s.Position = 0;
StreamReader reader = new StreamReader(s);
string jsonText = reader.ReadToEnd();
// Other code that converts json text to classes
}
catch (Exception e)
{
// .....
}
}
}
}
7. "Postman". Click "SEND";
8. The result, see the picture "- = RESULT = -";
Server error in application '/'.
Could not find this resource.
Description: HTTP 404. The resource (or one of its dependencies) may have been deleted, received a different name, or may be temporarily unavailable. Review the following URL and verify that it is correct.
Requested URL: / Hook
Version Information: Microsoft .NET Framework Version 4.0.30319; ASP.NET version: 4.7.3062.0
Update_1
我用的是linkhttp://localhost:44836/api/Hook
代码不会在 breakpoint
.
处停止
结果:
{<br>
"Message": "Could not find the HTTP resource corresponding to the request URI \" http://localhost:44836/api/Hook\".",
"MessageDetail": "Could not find the type corresponding to the controller \" 挂钩“.”
}
我用的是linkhttp://localhost:44836/Hook/ViberProcess
代码不会在 breakpoint
.
处停止
结果
Server error in application '/'.
For this object, no parameterless constructors are defined.
Description: An unhandled exception occurred during the execution of the current web request.
Examine the stack trace for more information about this error and the code snippet that caused it.
Exception Details: System.MissingMethodException: No parameter-less constructors are defined for this object.
Source Error:
An unhandled exception occurred during the execution of the current web request.
Information on the origin and location of the exception can be obtained using the following exception stack trace.
只需删除 ViberProcess
操作中的 HttpContext context
。
所以,方法会变成
public IActionResult ViberProcess()
{
Stream s = HttpContext.Current.Request.InputStream;
//... continue your code from here.
}
这背后的原因是,您提到 HttpContext context
作为 ViberProcess
的参数,但是当您发送请求时,它将使用确切的架构进行搜索。
因此,在您的请求中,您不能从任何地方传递 HttpContext
。所以,这个请求将永远找不到。
截图如下:
试试这个,如果还有问题请告诉我。
我需要 "WinForm" 申请 viber 通讯。
"Webhook" 计划从 viber 接收数据(事件),然后数据将在应用程序 "WinForm" 中使用。
我做到了:
- 创建项目"ASP.NET Web Application (.NET Framework)";
- 选择模板 - "Empty" + "MVC" + "API";
- 添加了控制器"Controller MVC 5 - empty"。控制器名称"HookController";
- 我运行申请"Postman";
- "Postman"。我设置请求"POST";
- "Postman"。我设置了 link
http://localhost:44836/Hook
; - "Postman"。点击【=113=】;
- 结果,见图"-=RESULT=-";
如果我对理论理解正确,那么在执行"Postman" action. I click "SEND"
之后,ViberProcess (HttpContext context)
方法应该在HookController.cs
控制器中执行,代码应该停止在[=16] =].
这不会发生。
文档 Viber REST API
- link
问题。
如何制作Webhook?
代码HookController.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//
using System.Runtime.Remoting.Contexts;
namespace WebAppl1.Controllers
{
public class HookController : Controller
{
// GET: Hook
//public ActionResult Index()
//{
// return View();
//}
[HttpPost]
// [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void ViberProcess(HttpContext context)
{
try
{
Stream s = context.Request.InputStream;
// Stream s = Context.Request.InputStream;
// or Stream s = HttpContext.Current.Request.InputStream;
s.Position = 0;
StreamReader reader = new StreamReader(s);
string jsonText = reader.ReadToEnd();
// Other code that converts json text to classes
}
catch (Exception e)
{
// .....
}
}
}
}
7. "Postman". Click "SEND";
8. The result, see the picture "- = RESULT = -";
Server error in application '/'.
Could not find this resource.
Description: HTTP 404. The resource (or one of its dependencies) may have been deleted, received a different name, or may be temporarily unavailable. Review the following URL and verify that it is correct.
Requested URL: / Hook
Version Information: Microsoft .NET Framework Version 4.0.30319; ASP.NET version: 4.7.3062.0
Update_1
我用的是linkhttp://localhost:44836/api/Hook
代码不会在 breakpoint
.
处停止
结果:
{<br>
"Message": "Could not find the HTTP resource corresponding to the request URI \" http://localhost:44836/api/Hook\".",
"MessageDetail": "Could not find the type corresponding to the controller \" 挂钩“.”
}
我用的是linkhttp://localhost:44836/Hook/ViberProcess
代码不会在 breakpoint
.
处停止
结果
Server error in application '/'.
For this object, no parameterless constructors are defined.
Description: An unhandled exception occurred during the execution of the current web request.
Examine the stack trace for more information about this error and the code snippet that caused it.
Exception Details: System.MissingMethodException: No parameter-less constructors are defined for this object.
Source Error:
An unhandled exception occurred during the execution of the current web request.
Information on the origin and location of the exception can be obtained using the following exception stack trace.
只需删除 ViberProcess
操作中的 HttpContext context
。
所以,方法会变成
public IActionResult ViberProcess()
{
Stream s = HttpContext.Current.Request.InputStream;
//... continue your code from here.
}
这背后的原因是,您提到 HttpContext context
作为 ViberProcess
的参数,但是当您发送请求时,它将使用确切的架构进行搜索。
因此,在您的请求中,您不能从任何地方传递 HttpContext
。所以,这个请求将永远找不到。
截图如下:
试试这个,如果还有问题请告诉我。