X509Certificate2 cer = new X509Certificate2(@"path", "********",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
HttpClient httpClient = new HttpClient(handler);
var stringContent = new StringContent(requestXml, System.Text.Encoding.UTF8);
var req = await httpClient.PostAsync("https://api.mch.weixin.qq.com/secapi/pay/refund", stringContent);

  

 private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
} ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;

  

update: I ran the exact same thing on Windows 7 and it worked exactly as needed.

// using System.Net.Http;
// using System.Security.Authentication;
// using System.Security.Cryptography.X509Certificates;

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(new X509Certificate2("cert.crt"));
var client = new HttpClient(handler);
var result = client.GetAsync("https://apitest.startssl.com").GetAwaiter().GetResult();

var clientCertificate = await HttpContext.Connection.GetClientCertificateAsync();

if(clientCertificate!=null)
            return new ContentResult() { Content = clientCertificate.Subject };

https://blog.pedrofelix.org/2012/12/16/using-httpclient-with-ssltls/

HttpClientHandler and WebRequestHandler.

The first option is to explicitly configure the HttpClient with a HttpClientHandler instance, containing its ClientCertificateOptions property set to Automatic.

var client = new HttpClient(

new HttpClientHandler{
   ClientCertificateOptions = ClientCertificateOption.Automatic
});
// ...

For classical scenarios (e.g. console, WinForms or WPF applications) there is a second option using the WebRequestHandler, which provides more control over the configuration.

var clientHandler = new WebRequestHandler()
clientHandler.ClientCertificates.Add(cert);
var client = new HttpClient(clientHandler)

where cert is a X509Certificate2 instance representing the client certificate.
This instance can be constructed directly from a PFX file or obtained from a Windows certificate store

X509Store store = null;
try
{
store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
// select the certificate from store.Certificates ...
}
finally
{
if(store != null) store.Close();
}

httpclient cer的更多相关文章

  1. HttpClient相关

    HTTPClient的主页是http://jakarta.apache.org/commons/httpclient/,你可以在这里得到关于HttpClient更加详细的信息 HttpClient入门 ...

  2. HttpClient,DefaultHttpClient使用详解

    HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=new DefaultHttpClient(); 发送GET请 ...

  3. httpclient详细介绍

    1.HttpClient简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...

  4. HttpClient入门

    HttpClient入门 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 H ...

  5. android httpClient 支持HTTPS的2种处理方式

    摘自: http://www.kankanews.com/ICkengine/archives/9634.shtml 项目中Android https或http请求地址重定向为HTTPS的地址,相信很 ...

  6. android httpClient 支持HTTPS的访问方式

    项目中Android https请求地址遇到了这个异常,javax.net.ssl.SSLPeerUnverifiedException: No peer certificate,是SSL协议中没有终 ...

  7. HttpClient使用详解

    http://itindex.net/detail/52566-httpclient HttpClient使用详解 标签: httpclient | 发表时间:2015-01-22 12:07 | 作 ...

  8. HttpClient请求详解

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  9. Android进阶(三)android httpClient 支持HTTPS的访问方式

    项目中Android https请求地址遇到了这个异常(无终端认证): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 是S ...

随机推荐

  1. Linux进程管理之ps

    Linux 是一种动态系统,能够适应不断变化的计算需求.下面介绍一些 Linux 所提供的工具来进行进程的查看与控制,掌握这些让我们能在某些进程出现异常的时候及时查看相关的指标,从而解决问题. 进程管 ...

  2. 配置管理-git研究(版本管理)

    1. 安装git2.7 git2.7具体安装步骤如下: [root@host1 ~]# yum install curl-devel expat-devel gettext-devel openssl ...

  3. NBU恢复数据库数据文件报错RMAN-06091

    RMAN-06091: no channel allocated for maintenance (of an appropriate type) 一.错误信息 报错信息如下 Starting res ...

  4. 题解:洛谷P1891 疯狂LCM

    原题链接 题目描述 描述: 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N) ...

  5. centos7删除Apache组件

    非特殊需要不要删除centos7中Apache等组件!首先查看centos中Apache版本(前面我们说了centos7删除PHP,centos7删除MariaDB,可能很多朋友会有疑问为什么要把所有 ...

  6. Python GIL、CPU密集型、IO密集型

    Python GIL(Global Interpreter Lock(全局解释器锁)) 1:进程里面多个线程,线程 共享A=10 2:Python解释器,A改完值之后会传回进程容器,为了防止A和B同时 ...

  7. Shell里的特殊符号

    Shell里的特殊符号: (1)单引号: 由单引号括起来的符号都作为普通字符处理,他们都失去了特殊意义. (2)双引号: 除美元符号($).倒引号.反斜线(\)仍保留特殊含义外,其余符号都作为普通字符 ...

  8. C#中的Queue集合

    Queue<T>集合 特点:先进先出,简单来说,就是新添加的元素是顺序添加在集合尾部,但是,移除的时候是从顶部开始移除元素. 三个方法: Enqueue(T obj);//顺序添加一个值到 ...

  9. 一篇文章看懂mysql中varchar能存多少汉字、数字,以及varchar(100)和varchar(10)的区别

    看完这篇文章,你能搞清楚以下问题: 1.varchar(100)和varchar(10)的区别在哪里? 2.varchar能存多少汉字.数字? 3.varchar的最大长度是多少呢? 4.字符.字节. ...

  10. 华为鸿蒙OS发布!方舟支持混合编译,终将可替换安卓?

    前言 有关于鸿蒙的消息之前也有说过,就在昨天下午,华为举行了2019开发大会,正式推出了鸿蒙os系统(Harmony).其相关负责人表示,也是基于微软内核的全场景分布式OS   鸿蒙凭借微内核的优势, ...