我正在尝试做一个简单的 Azure 函数来了解它.将有3个功能:
I'm trying to do a simple Azure Function to learn about it. There will be 3 functions:
我已经完成了第一个和第三个.但我不能通过 POST 传递参数.我一直在寻找示例,但无法成功运行它们.客户端应用程序是一个 Windows 窗体.
I've been able to do the first and the third ones. But I can't pass the parameter by POST. I've looked for examples but I couldn't run them with success. The client app is a Windows Forms one.
谁能给我一个例子,说明如何通过 POST 将参数传递给函数以及如何读取它们?
Could anyone show me an example anout how to pass parameters by POST to the function and how to read them?
提前致谢
这是通过 GET 传递参数的代码(这工作正常):
Here's the code to pass the parameters by GET (this is working fine):
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);
}
}
}
这是接收它并进行插入的代码(这也可以):
And here's the code to receive it and do the insert (this is working too):
[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;
}
}
我正在寻找的是通过 POST 来解决这个问题
What I'm looking for is to to this by POST
要从请求体(post request)中获取请求内容,可以使用 req.Content.ReadAsAsync 方法.这是代码示例.
To get the request content from the request body(post request), you could use req.Content.ReadAsAsync method. Here is the code sample.
示例请求正文.
{
"name": "Azure"
}
定义一个类来反序列化发布数据.
Define a class to deserialize the post data.
public class PostData
{
public string name { get;set; }
}
获取帖子数据并显示.
PostData data = await req.Content.ReadAsAsync<PostData>();
log.Info("name:" + data.name);
发送发布请求的客户端代码.
Client side code to send the post request.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("function-url");
req.Method = "POST";
req.ContentType = "application/json";
Stream stream = req.GetRequestStream();
string json = "{"name": "Azure" }";
byte[] buffer = Encoding.UTF8.GetBytes(json);
stream.Write(buffer,0, buffer.Length);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
这篇关于如何通过 POST 将参数传递给 Azure 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!