0

After deploying my ASP.NET Core 3.1 Web API to the ISS I am facing an exception:

One or more errors occured. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

When I walked through cases with similar exceptions in SO, there were mostly problems with PROXY settings. Here I am not using proxy. In my local machine everything works fine. Problem came when I deployed my Web API to the server, and I am not sure what might cause it.

Suspections:

  • wrongly configured HttpClient
  • not supported Core 3.1 by host (awaiting for feedback from support)

In my logs it is written that the exception is thrown in my service class by GetHtmlDocument method:

public class HttpClientProvider : IHttpClientProvider
    {
        private static HttpClient _httpClient;

        public string GetHtmlDocument(string url) //this method is throwing exception
        {
            string html = string.Empty;

            using (HttpResponseMessage response = GetHttpClient().GetAsync(new Uri(url, UriKind.Absolute)).Result)
            {
                using (HttpContent content = response.Content)
                {
                    html = content.ReadAsStringAsync().Result;
                }
            }

            return html;
        }

        private HttpClient GetHttpClient()
        {
            if (_httpClient != null)
            {
                return _httpClient;
            }

            return CreateNewHttpClient();
        }

        private static HttpClient CreateNewHttpClient()
        {
            HttpClientHandler handler = new HttpClientHandler();
            _httpClient = new HttpClient(handler);
            _httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3703.0 Safari/537.36");

            return _httpClient;
        }
    }

What do you think?

bakunet
  • 191
  • 10

0 Answers0