0

.Net Framework: 4.8 Windows Server: 2012

Below code written in Console Application while i ran simple it's working fine. But when i schedule same console application in Task Scheduler it's Throwing Error.

The underlying connection was closed. An Existing connection was forcibly close by Remote Host

                handler.ClientCertificates.Add(new X509Certificate2(CertificateFile, PassPhrase));                             

                var client = new HttpClient(handler);
                var authToken = Encoding.ASCII.GetBytes($"{USERNAME}:{PASSWORD}");

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));

                var requestContent = new StringContent("", Encoding.UTF8, "application/json");
                var response = client.PostAsync(RestAPIURL, requestContent).Result;

                response.EnsureSuccessStatusCode();
                Log.WriteLog(response.ToString());                

                resData = response.Content.ReadAsStringAsync().Result;
                resData.WriteResponse(); ```
Liam
  • 27,717
  • 28
  • 128
  • 190
Nikunj Nandaniya
  • 65
  • 1
  • 1
  • 5
  • 1
    Does this answer your question? [An existing connection was forcibly closed by the remote host](https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host) – Liam Sep 21 '21 at 08:40
  • 1
    Your question very likely is **not related** to task scheduler. Task scheduler only runs executables under a specific configuration/schedule. It may be due to an incorrect (wrong permissions) user. You lack a bit of troubleshooting. – Cleptus Sep 21 '21 at 08:41

0 Answers0