繁体   English   中英

如何调用 Azure HTTP 触发器函数

[英]How to invoke an Azure HTTP trigger function

我有一个 Azure HTTP 触发器函数,它可以作为浏览器中的 URL 或通过 Postman 令人满意地调用。 如何从 C# 调用它?

功能是

    public static class FunctionDemo
{
    [FunctionName("SayHello")]
    public static async Task<IActionResult> SayHello(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
    {
        log.LogInformation("C# HTTP trigger function 'SayHello' processed a request.");

        return new OkObjectResult("Hello world");
    }
}

我使用它的代码是

        private void button1_Click(object sender, EventArgs e)
    {
        String url = "https://<app-name>.azurewebsites.net/api/SayHello";

        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();

        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string reply = reader.ReadToEnd();
        label1.Text = reply;
        dataStream.Close();
    }

request.GetResponse()的调用失败并且 Visual Studio 报告:

System.Net.WebException - The underlying connection was closed: An unexpected error occurred on a send.

Inner Exception 1:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Inner Exception 2:
SocketException: An existing connection was forcibly closed by the remote host

根据thisthis SO答案,可能是客户端和服务器之间的安全协议可能不匹配的情况,因此它会抛出您看到的错误。 在调用request.GetResponse()之前,您可以尝试将 securityProtocol 设置为您的客户端框架可以支持的 Tsl 版本。

像这样的东西——

WebRequest request = WebRequest.Create(url);

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 
                                       SecurityProtocolType.Tls11 |
                                       SecurityProtocolType.Tls12;

WebResponse response = request.GetResponse();

此外,根据MS 评论,您应该转向使用HttpClient而不是WebRequest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM