HttpClient - 连接在 VSTO 中关闭,而不是在控制台中

HttpClient - Connection closed in VSTO and not in console

我想从两个不同的地方执行同一段代码。

代码:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "...");
var post = client.PostAsync("https://mycompany.com/...", new StringContent(json, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();
var str = post.Content.ReadAsStringAsync().GetAwaiter().GetResult();

将此代码放入控制台时,它工作正常,我得到了预期的结果(str 中的 Json)。

但是当把它放在 Outlook VSTO 项目中时(在功能区部分)。它无法系统地告诉我连接已经关闭:

The underlying connection was closed: The connection was closed unexpectedly

在 VSTO 中,我试过:

另一方面,当请求另一个网站内容时,它有效(即:我更改了 http://mycompany.com to https://google.com)。

有什么想法吗?

我在 Outlook VSTO 中遇到了与 HttpClient 类似的问题,我添加了

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

帮助我解决了问题。

可能会尝试 SecurityProtocol