c# - 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 中,我尝试过:
- 使用没有授权标头的 GetStringAsync() 没有成功
- 添加
client.CancelPendingRequests();
不成功
另一方面,当询问另一个网站内容时,它可以工作(即:我将http://mycompany.com更改为https://google.com)。
任何想法?
解决方案
HttpClient
我在 Outlook VSTO 中遇到了类似的问题,我添加了
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
这帮助我解决了这个问题。
可能尝试玩SecurityProtocol
.
推荐阅读
- r - 关于从 COVID19 WHO 数据集生成条形图的问题
- reactjs - 组件仅在包装在 `div` 中时才会更新
- php - 如何在laravel中获取选择框的多个选定值?
- machine-learning - 如何改进消费者支出调查的预测?
- java - 为什么在 Maven 中添加依赖项会触发功能?
- sql - 错误:表“p”缺少 FROM 子句条目
- html - 溢出-x:自动创建不需要的垂直滚动条
- python - 如何通过一次执行所有运算来加快 pandas.Series 上的多个算术运算?
- angular - 用户单击按钮后,将对象数组传递到 IONIC Angular 中的下一页
- reactjs - TS2339:“DefaultRootState”类型上不存在属性“tsReducer”