c# - Azure Http 触发函数调用

标签 c# azure azure-functions azure-function-app-proxy

我想要一个 Http 触发器函数来调用另一个 Http 触发器函数。 基本上,我尝试通过 URL(HTTP 请求)访问触发器 1,触发器 1 将调用触发器 2。 我的想法是输入触发器 2 的修复 URL,这样你只需调用触发器 1。 有什么想法如何做到这一点吗?

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    name = name ?? data?.name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

非常感谢任何帮助。

最佳答案

您可以使用HttpClient来执行普通的HTTP请求。调用函数如下所示:

static HttpClient client = new HttpClient();
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req)
{
    var url = "https://<functionapp>.azurewebsites.net/api/Function2?code=<code>";
    var response = await client.GetAsync(url);
    string result = await response.Content.ReadAsStringAsync();
    return req.CreateResponse(HttpStatusCode.OK, "Function 1 " + result);
}

关于c# - Azure Http 触发函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464213/

相关文章:

c# - 使用 HTML Agility Pack 和 xpath 将 "iso-8859-1"转换为 "utf-8"

c# - 有没有办法在 .editorconfig 文件中定义所需的类成员顺序?

c# - 如何使 ListBox 中的某些项目变为粗体?

azure - 当调用部署在 Azure 实例中的同一个 OpenAI GPT 模型时,如何单独跟踪每个程序的费用?

Azure Functions blob 触发器比 WebJobs blob 触发器更可靠?

c# - 使用 HTTP 触发器将 Azure Function 连接到 Azure 存储 Blob

JavaScript 日期格式转换。

azure - Azure 文件存储中的检测更改

c# - 使用 Azure 数据工厂从 C# 代码运行 U-SQL 脚本

azure - 如何使用 Azure Functions 代替 WebJobs?