[C#] System.Net.WebException: 要求は中止されました: SSL/TLS のセキュリティで保護されているチャ ネルを作成できませんでした

inno
2021-08-31 18:21 2983 12

System.Net.WebException: 要求は中止されました: SSL/TLS のセキュリティで保護されているチャ ネルを作成できませんでした

 

のエラーが発生した時の対応方法

このエラーが発生した時は以下の部分を追加したらできます。

 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

 

こんな感じで作成します。

 

 

        private static void SendData()

        {

            // HttpClientHandlerにProxy情報を設定する

            HttpClientHandler ch = new HttpClientHandler();

 

            // HttpClientHandlerを用いてHttpClientを生成

            HttpClient client = new HttpClient(ch);

 

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

 

            try

            {

 

                var json = "{}";

 

 

                var content = new StringContent(json, Encoding.UTF8, @"application/json");

 

                // POSTでレスポンスを取得

                var task = client.PostAsync("https://api.innoya.com/SendData", content);

                task.Wait();

                Console.WriteLine(task.Result);

                Console.ReadKey();

 

 

            }

            catch (Exception e)

            {

                System.Console.WriteLine(e.ToString());

            }

        }

コメント

2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)
2023年 12月 05日 (20:54)