gpt4 book ai didi

ssl - 请求被中止 : Could not create SSL/TLS secure channel in Windows 8 Metro App

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:31 24 4
gpt4 key购买 nike

我有一个包含 350 个可下载图片 URL 的列表。我通过运行多个任务一次并行下载 10 张图像。但是在下载了 N 个图像后,我的代码突然抛出以下异常。

Exception: "An error occurred while sending the request."

InnerException: "The request was aborted: Could not create SSL/TLS secure channel."

StackTrace: "at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n ...

我创建了一个示例项目 来重现此异常。我手里有 2 个测试用例。可以从My Sky Drive Here下载运行测试工程.右键单击文件 HTTPClientTestCases1and2.zip 并下载。

案例 1:使用单个实例 HttpClient 进行所有图片下载。

在这种情况下,我使用相同的 HttpClient 向 10 个 url 发送并行请求。在这种情况下,大部分时间下载都是成功的。上次成功下载图像后等待至少 40 秒(最多 1 分 40 秒)以发送下一批的下一个并行下载请求。由于此异常,一张图像肯定会失败。但是它写了很多地方并建议使用单个 HttpClient 进行多个请求。

   public async void DownloadUsingSingleSharedHttpClient(Int32 imageIndex)
{
Uri url = new Uri(ImageURLs[imageIndex]);

UnderDownloadCount++;

try
{
Byte[] contentBytes = null;

try
{
// Exception IS THROWN AT LINE BELOW
HttpResponseMessage response = await _httpClient.GetAsync(url);

contentBytes = await response.Content.ReadAsByteArrayAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Download Failed at GetAsync() :" + ex.Message);
throw ex;
}

DownloadedCount++;

if (OnSuccess != null)
OnSuccess(this, new DownloadSuccessEventArgs() { Index = imageIndex, Data = contentBytes });
}
catch (HttpRequestException hre)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(hre, null);
}
catch (TaskCanceledException hre)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(hre, null);
}
catch (Exception e)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(e, null);
}
}

案例 2:为每个图片下载创建新的 HttpClient 实例

在这种情况下,由于并行下载图像时出现相同的异常,它会非常频繁地失败。

public async void DownloadUsingCreatingHttpClientEveryTime(Int32 imageIndex)
{
Uri url = new Uri(ImageURLs[imageIndex]);

UnderDownloadCount++;
try
{
Byte[] contentBytes = null;

using (HttpClientHandler _handler = new HttpClientHandler())
{
_handler.AllowAutoRedirect = true;
_handler.MaxAutomaticRedirections = 4;

using (HttpClient httpClient = new HttpClient(_handler))
{
httpClient.DefaultRequestHeaders.ExpectContinue = false;
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "false");

try
{
// Exception IS THROWN AT LINE BELOW
contentBytes = await httpClient.GetByteArrayAsync(url.OriginalString);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Download Failed :" + ex.Message);
throw ex;
}
}

_handler.Dispose();
}

DownloadedCount++;

if (OnSuccess != null)
OnSuccess(this, new DownloadSuccessEventArgs() { Index = imageIndex, Data = contentBytes });
}
catch (HttpRequestException hre)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(hre, null);
}
catch (TaskCanceledException hre)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(hre, null);
}
catch (Exception e)
{
DownloadFailedCount++;
if (OnFailed != null)
OnFailed(e, null);
}
}

请在 MainPage.xaml.cs 中编辑以下函数以检查两种情况

 private void Send10DownloadRequestParallel()
{
for (Int32 index = 0; index < 10; index++)
{
Task.Run(() =>
{
Int32 index1 = rand.Next(0, myImageDownloader.ImageURLs.Count - 1);

UpdateDownloadProgress();

// Case 1: Download Using Single Shared HttpClient
// myImageDownloader.DownloadUsingSingleSharedHttpClient(index1);

// OR

// Case 2: Download Using Creating Http Client Every Time
myImageDownloader.DownloadUsingCreatingHttpClientEveryTime(index1);
});
}
}

我的问题:我做错了什么?通过克服此异常在 WinRT 中实现并行下载器的最佳方法是什么。

最佳答案

我运行了您的示例应用程序,但只在几种情况下出现错误:

  1. 当您的应用请求的图像不存在时,.NET HTTP 客户端会抛出异常。您的处理程序不能很好地处理这种情况,因为内部异常是 NULL。我不得不稍微调整一下代码:

    async void myImageDownloader_OnFailed(object sender, EventArgs e)
    {
    await App.CurrentDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, delegate
    {
    TimeSpan time =(DateTime.Now -dateTimeSuccess);

    String timeGap = "Ideal For:" + time.ToString() + "\n";
    ErrorListBox.Text += "\n Failed When: " + DownloadInfo.Text + "\n";
    ErrorListBox.Text += timeGap;

    // CX - added null check for InnerException, as these are NULL on HTTP result status 404
    var ex = sender as Exception;
    if (ex.InnerException != null)
    ErrorListBox.Text += ex.InnerException.Message;
    else
    ErrorListBox.Text += "Inner Exception null - Outer = (" + ex.ToString() + ")";
    });
    }
  2. 我唯一一次收到您的其他错误无法在 Windows 8 Metro 应用程序中创建 SSL/TLS 安全通道,是在我使用 HTTP 调试代理 (Fiddler) 时。如果我不使用拦截所有 HTTP(S) 调用的 Fiddler,那么我下载没有问题。我什至开始快速连续地进行多次下载(通过在一秒钟内多次单击蓝色下载区域)。结果是所有项目都已下载(除了 404 错误,如上所述)。

这是成功下载的屏幕截图(404 错误除外)。此屏幕截图正在运行测试用例 #2(HttpClient 的多个实例)。我确实运行了测试用例 #1(HttpClient 的单个实例),结果也成功了。

Test application screenshot

简而言之,我没有看到您遇到的问题。我唯一能想到的就是让您从不同的机器或位置试用您的应用程序。

关于ssl - 请求被中止 : Could not create SSL/TLS secure channel in Windows 8 Metro App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com