首页 > 解决方案 > HttpClient - 在 VSTO 中而不是在控制台中关闭连接

问题描述

我有相同的代码,我想从两个不同的地方执行。

编码 :

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();

将此代码放入控制台时,它可以正常工作,并且我得到了预期的结果(一个 Json in str)。

但是,将其放入 Outlook VSTO 项目时(在功能区部分)。它未能系统地告诉我连接已经关闭:

The underlying connection was closed: The connection was closed unexpectedly

在 VSTO 中,我尝试过:

另一方面,当询问另一个网站内容时,它可以工作(即:我将http://mycompany.com更改为https://google.com)。

任何想法?

标签: c#httpclientvsto

解决方案


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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

这帮助我解决了这个问题。

可能尝试玩SecurityProtocol.


推荐阅读