首页 > 编程语言 >C# 使用证书发起请求

C# 使用证书发起请求

时间:2024-02-23 11:13:39浏览次数:29  
标签:SslProtocols 请求 证书 C# client handler new null string

private HttpClientHandler getHandler()
{
    string cacheKey = "handlerChche";
    HttpClientHandler handler = null; //cache.GetCache<HttpClientHandler>(cacheKey);
    if (handler == null)
    {
        handler = new HttpClientHandler
        {
            ClientCertificateOptions = ClientCertificateOption.Manual,
            SslProtocols = SslProtocols.Tls12
        };
        //获取证书路径
        //商户私钥证书,用于对请求报文进行签名
        handler.ClientCertificates.Add(new X509Certificate2(_ecrtpath, "123456", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet));
        //handler.ClientCertificates.Add(new X509Certificate2(CertHelper.GetDebugPath(), CertHelper.GetPwd(), X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet));
        handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
        handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
        
        //cache.SetCache(cacheKey, handler);
    }
    return handler;
}
 public static  string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
 {            
     postData = postData ?? "";
     using (HttpClient client = new HttpClient(getHandler()))
     {
         client.Timeout = new TimeSpan(0, 0, timeOut);
         if (headers != null)
         {
             foreach (var header in headers)
                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
         }
         using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
         {
             if (contentType != null)
                 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

             //HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
             //return response.Content.ReadAsStringAsync().Result;
             return client.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;
         }
     }
 }

 

标签:SslProtocols,请求,证书,C#,client,handler,new,null,string
From: https://www.cnblogs.com/gxivwshjj/p/18029049

相关文章

  • R语言逻辑回归(Logistic Regression)、回归决策树、随机森林信用卡违约分析信贷数据集
    原文链接:http://tecdat.cn/?p=23344最近我们被客户要求撰写关于信用卡违约分析的研究报告,包括一些图形和统计输出。本文中我们介绍了决策树和随机森林的概念,并在R语言中用逻辑回归、回归决策树、随机森林进行信用卡违约数据分析决策树是由节点和分支组成的简单树状结构。根据每......
  • Eclipse配置Anaconda3
    参考1,参考2打开eclipse,Help->InstallNewSoftware->addPydevhttp://pydev.org/updates选择PyDevforEclipse接受安装信任......
  • Rocky9和CentOSStream9怎么设置固定IP地址
    Rocky9和CentOSStream9设置固定IP地址和CentOS不一样设置设置以下文件#cat/etc/NetworkManager/system-connections/ens3.nmconnection[connection]id=ens3uuid=322d81cf-5218-304d-b537-ddbb2a0eed07type=ethernetautoconnect-priority=-999interface-name=ens3times......
  • background-position详解
    原文链接:https://blog.csdn.net/weixin_30687587/article/details/98437498一.background-position:lefttop;背景图片的左上角和容器(container)的左上角对齐,超出的部分隐藏。等同于background-position:0,0;也等同于background-position:0%,0%;二.background-position:r......
  • 查询pytest --version报错 AttributeError: module ‘collections‘ has no attribute
     卸载pytest及关联的插件先查询一下pytest及对应关联的插件 pipuninstallcoloramaexceptiongroupiniconfigpackagingpluggytomliallure-pytestpytest-allure-adaptorpytest回车,每次都回复Y,同意卸载 再重新安装pytestpipinstallpytest-ihttp://pypi.douba......
  • java中 Scanner类 使用相关情况
    1、Scanner类使用Scanner对象之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。◆基本语法:Scanners=newScanner(System.in);......
  • Go 100 mistakes - #61: Propagating an inappropriate context
       疑问:前两种情况(1.客户端连接中断2.HTTP请求取消)发生,publish却不expire也不会被cancel,这样会不会有问题? ......
  • vue3+vite 移动端适配postcss-pxtorem插件
    1、安装插件npmipostcss-pxtorem-D2、与package.json同级目录创建postcss.config.js文件module.exports={plugins:{autoprefixer:{overrideBrowserslist:["Android4.1","iOS7.1",......
  • docker常用命令
    docker教程1镜像相关1.1查看镜像列表sudodockerimages1.2镜像搜索比如搜索ubuntu基础镜像sudodockersearchubuntu可输出官方的镜像名称,以及star数量,选择一个需要的镜像下拉即可1.3下拉镜像sudodockerpull镜像名sudodockerpull镜像名:Tag1.4查看镜......
  • C#程序删除自身
    [DllImport("kernel32.dll")]publicstaticexternuintWinExec(stringlpCmdLine,uintuCmdShow);privatestaticvoidBeginKillSelf(){stringvBatFile=Path.GetDirectoryName(Application.ExecutablePath)+......