繁体   English   中英

如何在Mailgun的ASP.NET C#中收到HTTP POST?

[英]How can I receive a HTTP POST in ASP.NET C# from Mailgun?

http://documentation.mailgun.net/quickstart.html包含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 = 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错误代码。

谢谢你的帮助。

主要我希望我能在大约6小时前找到这个...

如果您使用mvc和剃刀视图引擎,则需要执行以下操作

    [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");

    }

我需要在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>

sms.aspx.cs

public partial class sms : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string subject = Request.Params["subject"];
    string message = Request.Params["body-plain"];

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["YOURCONNECTIONSTRING"].ConnectionString))
    {
        cn.Open();

        using (SqlCommand cm = cn.CreateCommand())
        {
            cm.CommandType = CommandType.Text;
            cm.CommandText = "INSERT INTO SMS (subject, message, DateTime) VALUES (@Subject, @Message, @Dateandtime);";
            cm.Parameters.Add("@Subject", SqlDbType.NVarChar).Value = subject;
            cm.Parameters.Add("@Message", SqlDbType.NVarChar).Value = message;
            cm.Parameters.Add("@Dateandtime", SqlDbType.DateTime).Value = DateTime.Now.ToString();

            SqlDataReader dr = cm.ExecuteReader();
            dr.Dispose();
            cm.Dispose();

        }

    }

}
}

希望这可以帮助其他人使用Mailgun和C#。

我的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM