Azure Function Unable to Connnect to Third Party API via httpclient.

Jessie Black 6 Reputation points
2022-10-14T16:26:57.027+00:00

I have a function app that works perfectly fine prior to publishing to Azure.

I am trying to connect to a third party API to receive updates on usage for the application.

The httpclient fails after publishing the project to Azure but works perfectly fine prior to publishing.
Here is the trace when the PostAsync request is sent.

System.AggregateException: One or more errors occurred. (The SSL connection could not be established, see inner exception.)
---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Int32>.GetResult(Int16 token)
at System.Net.Security.SslStream.<FillHandshakeBufferAsync>g__InternalFillHandshakeBufferAsync|189_0TIOAdapter
at System.Net.Security.SslStream.ReceiveBlobAsyncTIOAdapter
at System.Net.Security.SslStream.ForceAuthenticationAsyncTIOAdapter
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation1.WaitWithCancellationAsync(CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)

I'm really at a loss as to how to resolve this and have done quite a bit of digging around for answers. Any help is greatly appreciated.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,263 questions
{count} vote

2 answers

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,421 Reputation points
    2022-11-02T07:22:23.463+00:00

    Hi @Jessie Black ,

    From the error , this appears to be a ssl handshake issue. It might be working on your local machine because your machine and the third party API may be in same network or the Azure function might be integrated with vnet and it is not able to communicate with the 3rd party API. If the function app is integrated with vnet, please disconnect and try to run the function to check if it works. I would also suggest you to look at the network configuration of the 3rd party API to see if they have any IP/network restrictions.

    Hope this helps! Feel free to reach out to me if you have any questions.


  2. Yash Mochi 41 Reputation points
    2022-12-08T11:17:19.223+00:00

    @MughundhanRaveendran-MSFT ,

    I am also facing same issue which @Jessie Black and @Karpagam Gurumurthy are facing.
    I am having site-to-site connection between Azure Network and our On-Prem network.
    When I am trying to access On-Prem network APIs, it is throwing same (SSL connection reset) error.
    I have tried setting WEBSITE_VNET_ROUTE_ALL = 1 and also tried creating a VM in Azure environment and sending a CURL request to On-Prem network.
    From VM as well it is throwing SSL Connection reset error.
    There is no restriction from On-Prem side, I am able to test connection using Azure VM powershell and telnet from Azure VM on required ports.

    Please update if any solution found or if any workaround.