Could not create SSL/TLS secure channel error when execute System.Net.WebClient.DownloadFile?

Hi There,

I have developed a Windows Service Application and my part of coding is given below. I got the below-mentioned exception (Refer to Error log details title) when running the line starts with Client.DownloadFile(..) though included the below lines. This error is occurring when trying to download the attachment file from the JIRA task.

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I am running this service in the Windows Server 2012 R2 machine.

Note: This issue occurs only in the Windows Server machine. Not in any other machine.

  • Code: *

Key Points:
→ The argument variable path in the below code is the link of the document attached to the JIRA task.
→ The argument variable id is the JIRA task id.
→ Service.UserName indicates JIRA admin username
→ Service.Password indicates JIRA admin password

public static bool DownloadDocument(string path, string id)
{
try
{
string FileName = path.Split(‘/’);
WebClient Client = new WebClient();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string DocumentLocation = Service._currentDirectoryPath + @“Attachments”;
string Url = path;

if (System.IO.Directory.Exists(DocumentLocation) == false)

{ System.IO.Directory.CreateDirectory(DocumentLocation + “\” + id); }

else

{ System.IO.Directory.Delete(DocumentLocation, true); System.IO.Directory.CreateDirectory(DocumentLocation + “\” + id); }

byte _crdential = UTF8Encoding.UTF8.GetBytes(Service.UserName + “:” + Service.Password);
Client.Headers[HttpRequestHeader.Authorization] = string.Format("Basic

{0}

", Convert.ToBase64String(_crdential));
Client.DownloadFile(new Uri(Url), DocumentLocation + "
" + id + "
" + FileName[FileName.Count() - 1]);

return true;
}
catch (Exception exc)

{ LogError.LogMessageToFile(id + " - Exception in DownloadDocument() - " + exc.Message + Environment.NewLine + exc.StackTrace); return false; }

}

  • Error log details: *

11/6/2019 6:38:55 PM: BRD-21817 - Exception in DownloadDocument() - The request was aborted: Could not create SSL/TLS secure channel.
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at Publish_Service.DocumentHandle.DownloadDocument(String path, String id).

11/6/2019 6:38:55 PM: Exception in PublishDetails() - Could not find file ‘D:\Publish_Service\brd-21817.html’.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream…ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader…ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamReader…ctor(String path, Encoding encoding)
at HtmlAgilityPack.HtmlDocument.Load(String path)
at Publish_Service.DocumentHandle.CheckH1ElementOccurance(String id)
at Publish_Service.Service.d__20.MoveNext().

I have tried the solutions suggested in the below links. But not helps. Still, the issue occurring when I run this service.

Any help on this would be greatly appreciated.

Regards,
Vadivel Natarajan