简体   繁体   中英

HttpRequest on Azure Function doesn't work with https

I've created a simple Azure Function (Http trigger) which I want to call through C# code. I understand that in order to call it, I have to perform an http request using the function URL displayed in the Azure Portal.
However, I'm getting an HttpRequestException (error occured while sending the request), when the GetAsync(...) method is reached. This is my code:

public static void Main(string[] args)
{
        AsyncMain();
        Console.ReadKey();
}

static async void AsyncMain()
{
        HttpResponseMessage response = await client.GetAsync("https://someapp.azurewebsites.net/api/somefunction?name=somevalue");
        response.EnsureSuccessStatusCode();
        string responseString = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseString);
}

When I change the URL string in the Code from https to http (http:// someapp.azurewebsites[...]), it works fine. There aren't any errors when running the function directly from a browser window either. So I suspect there's something wrong with my request.

Can someone tell me how I can perform HTTPS requests on my function using the correct link I got from the Azure Portal?
Thanks in advance!

Summarize the comments as answer for other communities reference:

Add the code ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; in application application startup. Because Azure Functions speaks TLS 1.2 and your project probably targets .NET 4.5 or 4.6 which defaults to TLS 1.0, so the handshake fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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