我已经开发了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,Boolean checkHost)
at System.IO.StreamReader..ctor(String path,Encoding encoding,Boolean detectEncodingFromByteOrderMarks,Encoding encoding)
at HtmlAgilityPack.HtmlDocument.Load(String path)
at Publish_Service.DocumentHandle.CheckH1ElementOccurance(String id)
at Publish_Service.Service.<PublishDetails>d__20.MoveNext().
我尝试了以下链接中建议的解决方案。但没有帮助。尽管如此,当我运行此服务时仍会出现问题。
在此方面的任何帮助将不胜感激。
关于, 卡尔提