如何从Mailgun接收ASP.NET C#中的HTTP帖子?
包含Django中http处理程序的一些示例代码:如何从Mailgun接收ASP.NET C#中的HTTP帖子?,c#,.net,http,webhooks,C#,.net,Http,Webhooks,包含Django中http处理程序的一些示例代码: # Handler for HTTP POST to http://myhost.com/messages for the route defined above def on_incoming_message(request): if request.method == 'POST': sender = request.POST.get('sender') recipient = reques
# Handler for HTTP POST to http://myhost.com/messages for the route defined above
def on_incoming_message(request):
if request.method == 'POST':
sender = request.POST.get('sender')
recipient = request.POST.get('recipient')
subject = request.POST.get('subject', '')
body_plain = request.POST.get('body-plain', '')
body_without_quotes = request.POST.get('stripped-text', '')
# note: other MIME headers are also posted here...
# attachments:
for key in request.FILES:
file = request.FILES[key]
# do something with the file
# Returned text is ignored but HTTP status code matters:
# Mailgun wants to see 2xx, otherwise it will make another attempt in 5 minutes
return HttpResponse('OK')
ASP.NET C#中的等效项是什么
例如,我尝试了Request.Form[“sender”],但Mailgun日志记录了一个HTTP 500错误代码
谢谢您的帮助。我需要在page指令中将ValidateRequest设置为false 示例代码- sms.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sms.aspx.cs" EnableEventValidation="false" ValidateRequest="false" Inherits="sms" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<p>SMS</p>
</form>
</body>
</html>
希望这能帮助其他人使用Mailgun和C#。Main我希望我能在6小时前找到它 如果您使用mvc和razor视图引擎,则需要执行以下操作
[HttpPost]
[ValidateInput(false)]
public ActionResult GoTruckGo(FormCollection oColl)
{
try
{
string sender = Request.Unvalidated().Form["sender"];
string body = Request.Unvalidated().Form["body-plain"];
sendLog(body);
// do something with data
}
catch (Exception ex)
{
sendLog("entered catch = "+ ex.Message);
}
return Content("ok");
}
我的VB.Net版本
' Get form data?
Dim message As String = String.Empty
Dim mailgun As NameValueCollection = Request.Form
Dim key As String
Dim values() As String
For Each key In mailgun.Keys
values = mailgun.GetValues(key)
For Each value As String In values
' create a new string
message &= key & "-" & value & "|"
' MsgBox(key & " - " & value)
Next value
Next key
' save message
你检查了你所有的request.form集合了吗?看看是否有东西被退回了?@PaulMcCowat谢谢你的快速评论。老实说,我也很难调试它,因为它是Mailgun web服务,需要发布到一个网站,我必须不断尝试新的东西,然后每次都上传它。我正在尝试将发布的数据存储到数据库中。但是只要继续获取500个内部服务器错误。我们需要知道服务器错误实际上是什么。我已经将页面设置得非常简单:如果我使用web浏览器访问页面,它只需将两个值插入数据库表中即可正常工作。只有当web服务尝试发布时,才会出现问题。我已将页面上的EnableEventValidation设置为false,无效。我不知道怎样才能找到错误,很不幸。我使用了HTTP工具Fiddler将数据发布到我的表单中。仅当设置了内容长度0时,它才起作用。否则就会出现错误。我想这是因为IIS需要设置内容长度。有没有办法禁用此要求?这是否也是调用MailGun发送例程的页面?我试图理解如何告诉MailGun将响应发送到smtpClient1.send(mailMessage1)的位置。当消息失败时,我想知道原因。接下来,我需要学习如何应对失败。
' Get form data?
Dim message As String = String.Empty
Dim mailgun As NameValueCollection = Request.Form
Dim key As String
Dim values() As String
For Each key In mailgun.Keys
values = mailgun.GetValues(key)
For Each value As String In values
' create a new string
message &= key & "-" & value & "|"
' MsgBox(key & " - " & value)
Next value
Next key
' save message