linux下访问https协议做法

一、http://www.mono-project.com/FAQ:_Security

1、inux本身在发行的时候是不带任何证书的,这和windows平台似乎恰恰相反。不过有工具,允许我们自行安装证书。

 

                  mozroots --import /               ##导入证书

 

                    --ask-remove  /       ##仅删除时确认

 

                    --machine               ##保存在全局环境

 

              

mozroots --import  --sync

或者手动加入证书

certmgr -ssl https://t.open.com

2是直接在程序中加入代码默认此https协议是正确的

using System;
using System.Security.Cryptography.X509Certificates;


namespace SSLtest
{
    
    class MainClass
    {
        
        // callback used to validate the certificate in an SSL conversation
        private static bool ValidateRemoteCertificate(
            object sender,
            X509Certificate certificate,
            X509Chain chain,
            System.Net.Security.SslPolicyErrors policyErrors
        )
        {
            
            return true;
            /*
            if (Convert.ToBoolean(ConfigurationManager.AppSettings["IgnoreSslErrors"]))
            {
                // allow any old dodgy certificate...
                return true;
            }
            else
            {
                return policyErrors == SslPolicyErrors.None;
            }
            */
        } // End Function ValidateRemoteCertificate

        
        public static void Main (string[] args)
        {
            //System.Net.ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
            System.Net.ServicePointManager.ServerCertificateValidationCallback += (s,ce,ca,p) => true;
            
            string strResponse = null;
            try 
            {
                System.Net.WebRequest request = System.Net.HttpWebRequest.Create("https://encrypted.google.com/");
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse) request.GetResponse();
                System.IO.Stream stream = response.GetResponseStream();
                System.IO.StreamReader sr = new System.IO.StreamReader(stream);
                strResponse = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                stream.Dispose();
            }
            catch(System.IO.IOException ex) 
            { 
                Console.WriteLine("Failed to connect to URI"); 
                Console.WriteLine("Reason:"); 
                Console.WriteLine(ex.Message);
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Stacktrace:"); 
                Console.WriteLine(ex.StackTrace); 
            }
            catch(System.UriFormatException ex) 
            { 
                Console.WriteLine("Bad URI format"); 
                
                Console.WriteLine("Reason:"); 
                Console.WriteLine(ex.Message); 
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Stacktrace:"); 
                Console.WriteLine(ex.StackTrace); 
            }
        
            Console.WriteLine ("Read the following stream:");
            Console.WriteLine(strResponse);
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(" --- Press any key to continue --- ");
            Console.ReadKey();
        } // End Sub Main 
        
        
    } // End Class MainClass
    
    
} // End Namespace SSLtest

 

 

转载于:https://www.cnblogs.com/win-and-first/archive/2013/04/20/linuxhttps.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值