使用 .NET 4.0 或 4 时无法从 Twitch static-cdn 加载图像。5.X

Images are failing to load from the Twitch static-cdn when using .NET 4.0 or 4.5.X

我有一个 WPF 桌面应用程序,我使用它们 API 在 Twitch 上导航游戏和流媒体。大约 2 周前(mid/late 2018 年 2 月)我开始注意到流预览图像不再加载(<Image /> 标签在布局中不占用 space)。

图像加载非常简单。为了演示,我选择了 24/7 流。

<Image Source="https://static-cdn.jtvnw.net/previews-ttv/live_user_esl_sc2-640x360.jpg" />

这个 URL 来自 API 并且在浏览器中工作正常。在测试过程中,我创建了一个基本的 WPF 应用程序。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Source="https://static-cdn.jtvnw.net/previews-ttv/live_user_esl_sc2-640x360.jpg" />
    </Grid>
</Window>

这最终成功了,我最终意识到这是因为新项目的目标框架是 .NET 4.6.1。将其切换为 4.5.X 会导致它产生与其他应用程序相同的结果。将第一个应用程序切换到 4.6.X 会导致它再次运行。 .NET 4.0 也失败了。其他网站(例如 Imgur)可以正常运行。也许还值得注意的是,在任何 .NET 框架上使用 Twitch url 时,设计器 window 中没有预览图像,但 Imgur 有一个。

该问题出现在两台 Windows 10 设备和一台 Windows 7 设备上。我相信 Windows 10 台设备中的一台有定期 windows 更新(可能被禁用,不确定),而另外两台设备的更新设置为仅手动。

是什么导致了这种行为?

看来 Twitch 现在正在通过 HTTP/2 提供图像。我相信旧版 .NET 下的 HTTP 请求默认使用 SSL3,这被 Twitch 拒绝了。

除了将 .NET 版本升级到 4.6.x 或更高版本,设置安全协议也解决了问题:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;