zoukankan      html  css  js  c++  java
  • httpclient HttpWebRequest和restsharp 中使用https

    httpclient方式

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;

    private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
    return true;
    }

    HttpWebRequest 方式

    string host = @"https://localhost/";
    string certName = @"C: empcert.pfx";
    string password = @"password";

    try
    {
    X509Certificate2Collection certificates = new X509Certificate2Collection();
    certificates.Import(certName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

    ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
    req.AllowAutoRedirect = true;
    req.ClientCertificates = certificates;
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    string postData = "login-form-type=cert";
    byte[] postBytes = Encoding.UTF8.GetBytes(postData);
    req.ContentLength = postBytes.Length;
    
    Stream postStream = req.GetRequestStream();
    postStream.Write(postBytes, 0, postBytes.Length);
    postStream.Flush();
    postStream.Close();
    WebResponse resp = req.GetResponse();
    
    Stream stream = resp.GetResponseStream();
    using (StreamReader reader = new StreamReader(stream))
    {
        string line = reader.ReadLine();
        while (line != null)
        {
            Console.WriteLine(line);
            line = reader.ReadLine();
        }
    }
    
    stream.Close();
    

    }
    catch(Exception e)
    {
    Console.WriteLine(e);
    }

    restsharp方式

    var client = new RestClient(url);

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.DefaultConnectionLimit = 9999;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

    var certFile = Path.Combine(certificateFolder, "certificate.pfx");
    X509Certificate2 certificate = new X509Certificate2(certFile, onboard.authentication.secret);
    client.ClientCertificates = new X509CertificateCollection() { certificate };
    client.Proxy = new WebProxy();
    var restrequest = new RestRequest(Method.POST);
    restrequest.AddHeader("Cache-Control", "no-cache");
    restrequest.AddHeader("Accept", "application/json");
    restrequest.AddHeader("Content-Type", "application/json");
    restrequest.AddParameter("myStuff", ParameterType.RequestBody);
    IRestResponse response = client.Execute(restrequest);

  • 相关阅读:
    【python】python中的定义类属性和对像属性
    【Python】Python—判断变量的基本类型
    【python】Python中给List添加元素的4种方法分享
    【python】python各种类型转换-int,str,char,float,ord,hex,oct等
    【python】python 中的三元表达式(三目运算符)
    【python】 sort、sorted高级排序技巧
    【SQLAlchemy】SQLAlchemy技术文档(中文版)(中)
    【SQLAlchemy】SQLAlchemy技术文档(中文版)(上)
    【其他】VS提示不一致的行尾
    UML 之 用例图
  • 原文地址:https://www.cnblogs.com/wang2650/p/13840117.html
Copyright © 2011-2022 走看看