4

我有一个 Windows Store 应用程序,它是一个 WinRT 电话/桌面应用程序(即不是 UWP 应用程序),面向 Windows 8.1 及更高版本。

它已经在商店上架了好几年了,但最近它不再能够使用 HTTPS 连接各种 Web API 和网站(YouTube,以及我自己的网站)。

我也有这个应用程序的 WPF 版本,最近该应用程序也发生了这种情况,为了修复它,我使用了System.Net.ServicePointManager. 不幸的是,在我的 WinRT 环境中,System.Net不包括ServicePointManager. 在我的 WPF 应用程序中,我这样做了,并且效果很好:

ServicePointManager.ServerCertificateValidationCallback = delegate
{
    Debug.WriteLine("returning true (the ssl is valid)");
    return true;
};

// our server is using TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

在围绕 Internet 进行一些研究时,似乎 .NET 4.6 应该包含 .NET ServicePointManager,但我没有看到任何方法可以在 WinRT 开发环境中更改(甚至看到)我的 .NET 版本。

我又看了一些,发现 aStreamSocket可以用来连接 TLS1.2 ......但这似乎主要是为了启用蓝牙通信或与 Web 端点的通信,但只能通过主机名......这对我来说是不够的。我需要连接到一个实际的网站,而不仅仅是基础级域。

尝试这个,我做了以下事情:

StreamSocket socket = new StreamSocket();
string serverServiceName = "https";
socket.Control.KeepAlive = false;
url = "inadaydevelopment.com";
HostName serverHost = new HostName(url);
await socket.ConnectAsync(serverHost, serverServiceName, SocketProtectionLevel.Tls12);
text = await ReadDataFromSocket(socket);

如有必要,我可以包含代码ReadDataFromSocket(),但它似乎可以正常工作,当我将其指向https://google.com时,它可以按预期从套接字读取数据。但是,我似乎无法弄清楚如何将套接字指向任何有用的东西。inadaydevelopment.com 的主页不是我想要的;我希望使用托管在该服务器上的 Web API,但似乎找不到这样做的方法。

由于该ConnectAsync()方法的第一个参数是 just HostName,因此第二个参数 ( remoteServiceName) 必须是连接到我尝试连接的实际 API 或网页的方式。根据文档,也就是说,除了各种数值The service name or TCP port number of the remote network destination... 之外,我没有看到此参数的任何示例值,这https两者都不会让我访问我尝试连接的 API 端点或网页。

所以,有了那个超长的序言,我的问题归结为

有没有办法让我System.Net.ServicePointManager像在 WPF 应用程序中一样在我的 WinRT 应用程序中使用?如果是这样,怎么做?

如果没有,我该如何StreamSocket连接到我想连接的确切的 Web 服务或网页,而不仅仅是顶级主机?

如果这不可行,我还可以通过哪些其他方式使用 TLS1.2 消费 Web 内容?

提前感谢您的任何帮助或建议。

4

1 回答 1

2

使用Windows.Web.HttpAPI 而不是System.Net.HttpAPI。

System.Net.Http不支持 TLS1.2,但Windows.Web.Http在 WinRT 应用程序中支持。

于 2021-04-05T16:21:56.220 回答