本文介绍了如何在Mailgun的ASP.NET C#中收到HTTP POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含一些示例代码Django中的http处理程序:

http://documentation.mailgun.net/quickstart.html contains some example code for a http handler in Django:

    # 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#中的等价物是什么?

What is the equivalent in ASP.NET C#?

我尝试过Request.Form [sender],但是Mailgun日志记录了HTTP 500错误代码。

I have tried Request.Form["sender"] for example, but the Mailgun log records a HTTP 500 error code.

感谢您的帮助。

推荐答案

我需要在页面指令中将ValidateRequest设置为false。

I needed to set ValidateRequest to false in the page directive.

示例代码 -

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

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#。

Hope this helps someone else using Mailgun and C#.

这篇关于如何在Mailgun的ASP.NET C#中收到HTTP POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 11:16