Не удалось создать безопасный канал SSL / TLS при выполнении System.Net.WebClient.DownloadFile? - PullRequest
0 голосов
/ 07 ноября 2019

Я разработал приложение-службу Windows, и моя часть кодирования приведена ниже. Я получил упомянутое ниже исключение, когда запуск строки начинается с Client.DownloadFile(..), хотя он включает

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

Я запускаю эту службу на Windows Server 2012 R2 машине.

Примечание: Эта проблема возникает только на Windows Server машине. Ни в какой другой машине.

Код:

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;
        }
    }

Данные журнала ошибок:

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.<PublishDetails>d__20.MoveNext().

У меня естьпопробовал решения, предложенные в ссылках ниже. Но не помогает. Тем не менее проблема возникает при запуске этого сервиса.

Любая помощь по этому вопросу будет принята с благодарностью.

С уважением, Карти

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...