使用HttpWebRequest请求http链接时,无法访问的问题,设置ServicePointManager.SecurityProtocol安全协议

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                       | SecurityProtocolType.Tls
                                       | SecurityProtocolType.Tls11
                                       | SecurityProtocolType.Tls12;   
        [SecurityCritical]
        [SecuritySafeCritical]
        private static void InitialSecurityProtocol()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                              | SecurityProtocolType.Tls
                                              | (SecurityProtocolType)0x300 // Tls11
                                              | (SecurityProtocolType)0xC00 // Tls12
                                              | (SecurityProtocolType)0x3000; // Tls13
            }
            catch (Exception)
            {
                try
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                                  | SecurityProtocolType.Tls
                                                  | (SecurityProtocolType)0x300 // Tls11
                                                  | (SecurityProtocolType)0xC00; // Tls12
                }
                catch (Exception)
                {
                    try
                    {
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                                        | SecurityProtocolType.Tls
                                                        | (SecurityProtocolType)0x300; // Tls11
                    }
                    catch (Exception)
                    {
                        try
                        {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                                            | SecurityProtocolType.Tls;
                        }
                        catch (Exception) { }
                    }
                }
            }
        }

 

posted @ 2022-03-17 15:54  ₯㎕~  阅读(1147)  评论(0编辑  收藏  举报