<legend id='t1WiV'><style id='t1WiV'><dir id='t1WiV'><q id='t1WiV'></q></dir></style></legend>
    <i id='t1WiV'><tr id='t1WiV'><dt id='t1WiV'><q id='t1WiV'><span id='t1WiV'><b id='t1WiV'><form id='t1WiV'><ins id='t1WiV'></ins><ul id='t1WiV'></ul><sub id='t1WiV'></sub></form><legend id='t1WiV'></legend><bdo id='t1WiV'><pre id='t1WiV'><center id='t1WiV'></center></pre></bdo></b><th id='t1WiV'></th></span></q></dt></tr></i><div id='t1WiV'><tfoot id='t1WiV'></tfoot><dl id='t1WiV'><fieldset id='t1WiV'></fieldset></dl></div>

    1. <tfoot id='t1WiV'></tfoot>

        <bdo id='t1WiV'></bdo><ul id='t1WiV'></ul>
    2. <small id='t1WiV'></small><noframes id='t1WiV'>

    3. 如何通过 POST 将参数传递给 Azure 函数?

      How to pass parameters by POST to an Azure function?(如何通过 POST 将参数传递给 Azure 函数?)

        <tbody id='4DPj9'></tbody>
        <i id='4DPj9'><tr id='4DPj9'><dt id='4DPj9'><q id='4DPj9'><span id='4DPj9'><b id='4DPj9'><form id='4DPj9'><ins id='4DPj9'></ins><ul id='4DPj9'></ul><sub id='4DPj9'></sub></form><legend id='4DPj9'></legend><bdo id='4DPj9'><pre id='4DPj9'><center id='4DPj9'></center></pre></bdo></b><th id='4DPj9'></th></span></q></dt></tr></i><div id='4DPj9'><tfoot id='4DPj9'></tfoot><dl id='4DPj9'><fieldset id='4DPj9'></fieldset></dl></div>
          <tfoot id='4DPj9'></tfoot>
        • <small id='4DPj9'></small><noframes id='4DPj9'>

          • <bdo id='4DPj9'></bdo><ul id='4DPj9'></ul>
            • <legend id='4DPj9'><style id='4DPj9'><dir id='4DPj9'><q id='4DPj9'></q></dir></style></legend>

                本文介绍了如何通过 POST 将参数传递给 Azure 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试做一个简单的 Azure 函数来了解它.将有3个功能:

                I'm trying to do a simple Azure Function to learn about it. There will be 3 functions:

                • 1 向数据库表中插入一行的函数.此表将包含当前日期和用户键入并通过 GET 传递的字符串参数.
                • 1个功能与上一个类似,但通过POST传递参数.
                • 1 个函数用于读取表格并显示其内容.

                我已经完成了第一个和第三个.但我不能通过 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 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <bdo id='PW476'></bdo><ul id='PW476'></ul>

                  • <i id='PW476'><tr id='PW476'><dt id='PW476'><q id='PW476'><span id='PW476'><b id='PW476'><form id='PW476'><ins id='PW476'></ins><ul id='PW476'></ul><sub id='PW476'></sub></form><legend id='PW476'></legend><bdo id='PW476'><pre id='PW476'><center id='PW476'></center></pre></bdo></b><th id='PW476'></th></span></q></dt></tr></i><div id='PW476'><tfoot id='PW476'></tfoot><dl id='PW476'><fieldset id='PW476'></fieldset></dl></div>
                      <tbody id='PW476'></tbody>

                    • <legend id='PW476'><style id='PW476'><dir id='PW476'><q id='PW476'></q></dir></style></legend>
                      <tfoot id='PW476'></tfoot>

                          <small id='PW476'></small><noframes id='PW476'>