An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

Stevan
Stevan
Member
310 Points
20 Posts

I'm trying to fetch access token by authorization code for linkedin sign-in. It's working fine in locally but when deployed to server getting following error:

System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
---> 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: An existing connection was forcibly closed by the remote host

We have following code:

var inputParams = new Dictionary<string, string>
            {
                { "grant_type", "authorization_code" },
                { "code", authorizationCode },
                { "redirect_uri", redirectUri },
                { "client_id", appKey },
                { "client_secret", appSecret }
            };

var content = new FormUrlEncodedContent(inputParams);
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = client.PostAsync(authAccessTokenUrl, content).Result;
                var accessTokenResponse = response.Content.ReadAsAsync<AccessTokenResponse>().Result;
                accessTokenResponse.access_token;
            }
Views: 1457
Total Answered: 0
Total Marked As Answer: 0
Posted On: 08-Jan-2021 22:00

Share:   fb twitter linkedin
Answers
 Log In to Chat