我正在尝试做一个简单的Azure函数来了解它。将会有3个函数:如何通过POST将参数传递给Azure函数?
- 1函数向数据库的表中插入一行。该表将包含当前日期和由用户键入并通过GET传递的字符串参数。
- 1函数类似于上一个,但是通过POST传递参数。
- 1功能读取表格并显示其内容。
我已经能够做第一个和第三个。但我无法通过POST传递参数。我已经找过例子,但是我无法成功运行它们。客户端应用程序是Windows窗体。
任何人都可以告诉我一个例子:如何通过POST传递参数给函数以及如何读取它们?
感谢的提前
编辑:
下面的代码通过GET传递参数(这是工作的罚款):
private void button2_Click(object sender, EventArgs e)
{
string cadena = lsql1.Text + "?notas=" + tNotas.Text;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cadena);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("Grabado");
}
else
{
MessageBox.Show(res.StatusDescription);
}
}catch (WebException ex)
{
using (Stream s = ex.Response.GetResponseStream())
{
StreamReader sr = new StreamReader(s);
string text = sr.ReadToEnd();
text = text.Substring(1, text.Length - 2);
sr.Close();
text = text.Replace("\\", "");
text = "{" + text + "}";
Error mensajeError = JsonConvert.DeserializeObject<Error>(text);
MessageBox.Show(mensajeError.ExceptionMessage);
}
}
}
而这里的代码来接受它,做插入(这也是工作):
[FunctionName("sql1")]
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
log.Info("C# HTTP trigger function processed a request.");
var cnnString = "Server=SERVIDOR;Database=base_prueba;User ID =azure;Password=0000;Trusted_Connection=False;Encrypt=False;";
using (SqlConnection connection = new SqlConnection(cnnString))
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
DateTime fecha = DateTime.Today;
string notas = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "notas", true) == 0)
.Value;
// insert a log to the database
cmd.CommandText = "INSERT INTO Prueba_Azure (fecha, notas) VALUES ('" + fecha.ToString() + "', '" + notas + "')";
cmd.ExecuteNonQuery();
}
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
return name == req.CreateResponse(HttpStatusCode.OK, "Done");
}
catch (Exception ex)
{
HttpResponseMessage res = req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
return res;
}
}
我在找的是t他通过POST
请发表您的函数的例子至今。你在写什么语言?您可以在JavaScript中针对Node.js编写Azure函数,也可以在ASP.NET中编写C#:https://visualstudiomagazine.com/articles/2017/04/01/implementing-webhooks-azure-functions.aspx – Dai
对不起,我正在使用C#编写应用程序。 我已经添加了我所做的代码。 – davidrgh