[英]C# The SSL connection could not be established, see inner exception
我试图从支持 TLS 1.3 的站点下载文件。 我在下面收到错误。
System.Net.Http.HttpRequestException: 'The SSL connection could not be established, see inner exception.'
AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'DecryptError'.
我还尝试添加: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
我也尝试了其他协议,但正如我上面所说,网站支持 tls 1.3。 顺便说一下,当我删除并行 foreach 并运行时它运行良好。 但是,我想批量下载。
我使用 Parallel.ForEach 可能与此有关吗?
foreach (var directory in directories)
{
if (directory.Contains($"C:\\Websites\\{domain}\\Uploads") || directory.Contains($"C:\\Websites\\{domain}\\CustomCss"))
{
if (directory != null)
{
var directoryName = directory.Replace($"C:\\Websites\\{domain}\\", "/"); //String.Empty
if (directoryName.Contains("\\"))
{
directoryName = directoryName.Replace("\\", "/");
}
var createDirectory = await directoryClient.CreateDirectory(new CreateDirectoryRequest { FolderName = directoryName });
Parallel.ForEach(Directory.GetFiles(directory), new ParallelOptions { MaxDegreeOfParallelism = 15 }, async (item) =>
{
if (item != null)
{
var ms = new MemoryStream();
using (FileStream file = new FileStream(item, FileMode.Open, FileAccess.ReadWrite))
{
using (ms = new MemoryStream())
{
file.CopyTo(ms);
ms.ToArray();
}
}
string filename = "";
Uri uri = new Uri(item);
if (uri.IsFile)
{
filename = System.IO.Path.GetFileName(uri.LocalPath);
}
UploadFileResponse uploadResponse = new UploadFileResponse();
if (isImage(MimeMapping.MimeUtility.GetMimeMapping(item)))
{
byte[] imageContent = await DownloadAndCompareImages(domain, directoryName + "/" + filename, "90");
uploadResponse = await client.UploadFileAsync(new UploadFileRequest
{ FileContent = imageContent, FileName = "/" + directoryName + "/" + filename, ContentType = MimeMapping.MimeUtility.GetMimeMapping(ms.ToString()) });
}
else
{
uploadResponse = await client.UploadFileAsync(new UploadFileRequest
{ FileContent = ms.ToArray(), FileName = "/" + directoryName + "/" + filename, ContentType = MimeMapping.MimeUtility.GetMimeMapping(ms.ToString()) });
}
if (!uploadResponse.IsError)
{
finishedProcess++;
fileCounter++;
siteProcess.FinishedProcess = bucketName + " => Site is moving to MinIO...";
siteProcess.FinishedCount = finishedProcess + "/" + totalFileFolder.ToString();
siteProcess.MadePercentage = Convert.ToInt32((100 * (finishedProcess) / totalFileFolder));
}
else
{
isError = true;
}
if (finishedProcess > totalFileFolder)
{
finishedProcess = dataGridSites.SelectedItems.Count;
}
}
});
}
}
}
return isError;
}
其他function为:
private async Task<byte[]> DownloadAndCompareImages(string domain, string imgFilePath, string imgQuality)
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36");
Uri uriNormalPattern = new Uri($"https://{domain}/{imgFilePath}");
var uriWithoutQuery = uriNormalPattern.GetLeftPart(UriPartial.Path); //
var fname = System.IO.Path.GetFileName(uriWithoutQuery);
var imageBytesNormal = await httpClient.GetByteArrayAsync(uriNormalPattern); //
int imageWidth = GetImgWidth(imageBytesNormal);
var imageBytesCloudFlare = await GetCloudFlareImg(domain, imageWidth, imgQuality, imgFilePath);
if (imageBytesNormal.Length > imageBytesCloudFlare.Length)
{
return imageBytesCloudFlare;
}
else
{
return imageBytesNormal;
}
}
使用Parallel.ForEach
时,会在短时间内将大量请求发送到服务器。 这可能会使服务器不堪重负,而且HttpClient
在并行访问同一域时也会受到限制。
为了缓解这种情况,以下步骤可能会有所帮助:
HttpClient
实例,而应使用IHttpClientFactory
来创建实例。 有关详细信息,请参阅此链接。 这避免了“通过池化 HttpMessageHandler 实例的资源耗尽问题”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.