I have an Azure HTTP trigger function that can be invoked satisfactorily as a URL in a browser, or through Postman. How do call it from C#?
The function is
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");
}
}
My code to use it is
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();
}
The call to request.GetResponse() fails and Visual Studio reports:
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