[英]C# The SSL connection could not be established, see inner exception
I tried to download files from site which is support TLS 1.3.我试图从支持 TLS 1.3 的站点下载文件。 I got error below.
我在下面收到错误。
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'.
Also I tried to add: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
我还尝试添加:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
Also I tried other protocols but site is supporting tls 1.3 as i said above.我也尝试了其他协议,但正如我上面所说,网站支持 tls 1.3。 By the way, when I remove parallel foreach and run its working well.
顺便说一下,当我删除并行 foreach 并运行时它运行良好。 However, I would like to download as a bulk.
但是,我想批量下载。
I used Parallel.ForEach could it be related to this?我使用 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;
}
Other function is:其他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;
}
}
When using Parallel.ForEach
a lot of requests are sent to the server in a short period of times.使用
Parallel.ForEach
时,会在短时间内将大量请求发送到服务器。 This might overwhelm the server, but also HttpClient
is limited when accessing the same domain in parallel.这可能会使服务器不堪重负,而且
HttpClient
在并行访问同一域时也会受到限制。
In order to mitigate this, the following steps might help:为了缓解这种情况,以下步骤可能会有所帮助:
HttpClient
instance directly, you should use IHttpClientFactory
to create the instances.HttpClient
实例,而应使用IHttpClientFactory
来创建实例。 For details, see this link .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.