.NET下面的 .netfromwork使用和asp.net core下使用方式不一样
配置文件中代码:
public const string API_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
/// <summary> /// 本地或者服务器的证书位置 /// </summary> public const string CertPath = @"D:apiclient_cert.p12"; /// <summary> /// 本地或者服务器的证书密码 /// </summary> public const string CApassword = "9999999";
Build_Content()
这个主要是传递的参数封装值
public async Task<bool> Send_Transfer() { //HttpClient请求,在handler里添加X509Certificate2 证书,数据data是byte[] 类型,所以需要使用ByteArrayContent传入 var handler = new HttpClientHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Manual; handler.SslProtocols = SslProtocols.Tls12; //获取证书路径 //商户私钥证书,用于对请求报文进行签名 try { handler.ClientCertificates.Add(new X509Certificate2(WeixinPay.CertPath, WeixinPay.CApassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet)); } catch (Exception e) { _logger.LogError(e.Message); } handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; //post请求 var client = new HttpClient(handler); /////////////////////////////////////////////////////////// //以下属于在.netfromwork环境下写法 // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //调用证书 //X509Certificate2 cer = new X509Certificate2(WeixinPay.CertPath, WeixinPay.CApassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); //handler.ClientCertificates.Add(cer); /////////////////////////////////////////////////////////// response = await client.PostAsync(WeixinPay.API_URL, Build_Content()); /////////////////////////////////////////////////////////// return response.IsSuccessStatusCode; }