Dynamics 365Online V9.0 OAuth認證後調web api報基礎連接已關閉的問題

    在3年前我寫過一篇博文,介紹在online環境下如何在服務端使用oAuth身份驗證拿到token後調用web api,如果你還沒操作過並且感興趣的可以移步之前的博客,關於Azure中的一些操作的設置已經有些許不同,就懶得再去更新截圖了, 如發現和你的實際情況有出入,可以查看博文中鏈接的docs原文。

     今天在做一個POC,使用一個最新的9.1版本的Online拿到Token後去調用web api時,報了個基礎連接已經關閉的錯,錯誤見如下截圖,這在之前的8.2版本中沒碰到過,在online更新了大版本9.0以後也沒再經手過online的項目了。

 

 經過一翻gugou後,終於找到了辦法,原來是要強制指定安全協議,加上後就ok了

  using (HttpClient httpClient = new HttpClient())
                {
//添加如下的安全協議設置
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes  
                    httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", access_token);
                    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                    httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
                    
                    HttpResponseMessage response = httpClient.GetAsync(
                    "https://xx.crm5.dynamics.com/api/data/v9.1/accounts?$top=1").Result;
                    returnvalue = response.Content.ReadAsStringAsync().Result;
                }

 

參考博文:https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/263018/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-forcibly-closed-by-the-remote-host

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章