[英]How to identify the HTTP method (verb) used to invoke an Azure Function
[英]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
根據this和this 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.