Vasanth
Vasanth

Reputation: 1

Dynamics 365 Plugin call External REST API - SSL/TSL could not create channel issue

I have a task to call the external REST API to get data from third party application.

In that, i have created a C# Console application for tried this and it is working fine and i can get the data from thirty party application via REST API.

The same code used to tried in Dynamics Custom workflow\Plugin, i have got a error below. Please give your valuable suggestion on this.

"System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel."

Note: The below options are tried but no luck.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AlwaysGoodCertificate); ServicePointManager.Expect100Continue = true;

Thanks, Vasanth

Upvotes: 0

Views: 663

Answers (1)

Mohammad Atiour Islam
Mohammad Atiour Islam

Reputation: 5708

It is not clear how you are calling external API from D365 Plugin.

 // Call external API
 static async Task<bool> CallExternalAPI(Guid beziehungId)  
     {  
       
       bool status = false;  
       HttpClient apiClient = new HttpClient();  
       apiClient.DefaultRequestHeaders.Accept.Clear();  
       apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
       string url = @"https://reqres.in/api/users?page=2"; 
       HttpResponseMessage response = await apiClient.GetAsync(url);  
       if (response.IsSuccessStatusCode)  
       {  
         var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();  
       }  
       return status;  
     }  
     

If your problem with SSL/TLS secure channel you can use WebClient.

// Could not create SSL/TLS secure channel.
// Use (SecurityProtocolType)3072

 using (WebClient client = new WebClient())
 {
      
     client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError");
     ServicePointManager.Expect100Continue = true;
     //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
     ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

     
     htmlCode = client.DownloadString("MY LINK");

     //passing the URL again to function from which to extract the content and compare from above content.
     strNewCompanyCode = client.DownloadString("MY LINK");
 }   

Upvotes: 0

Related Questions