c# 微信发普通红包报证书错误
服务器已在mmc 控制里导入证书成功
代码里
//发送红包请求Post方法
postUrl=https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
menuInfo=生成的xml
public static string PostWebRequests(string postUrl, string menuInfo)
{
方法一
try
{
Encoding encoding = Encoding.UTF8;
byte[] bytes = encoding.GetBytes(menuInfo);
string cert = @"D:\cert\apiclient_cert.p12";//支付证书路径
string password = "";//支付证书密码
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert, password);
// cer.Subject 这里获取到了证书信息
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(postUrl);
webrequest.ClientCertificates.Add(cer);
webrequest.Method = "post";
webrequest.ContentLength = bytes.Length;
webrequest.GetRequestStream().Write(bytes, 0, bytes.Length);
HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream stream = webreponse.GetResponseStream();
string resp = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
resp = reader.ReadToEnd();
return resp;
}
}
catch (Exception ex)
{
return "";
}
方法二
//System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
//try
//{
// Encoding encoding = Encoding.UTF8;
// byte[] bytes = encoding.GetBytes(menuInfo);
// X509Store store = new X509Store("My", StoreLocation.LocalMachine);
// store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
// System.Security.Cryptography.X509Certificates.X509Certificate2 cert =
// store.Certificates.Find(X509FindType.FindBySubjectName, "", false)[0];
// HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(postUrl);
// webrequest.ClientCertificates.Add(cert);
// //webrequest.ClientCertificates.Add(cer);
// webrequest.Method = "post";
// webrequest.ContentLength = bytes.Length;
// webrequest.GetRequestStream().Write(bytes, 0, bytes.Length);
// webrequest.KeepAlive = true;
// HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
// Stream stream = webreponse.GetResponseStream(); /*CheckValidationResult的定义*/
// string resp = string.Empty;
// using (StreamReader reader = new StreamReader(stream))
// {
// resp = reader.ReadToEnd();
// return resp;
// }
//}
//catch (Exception exp)
//{
// return "";
//}
}
方法一 返回的错误
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[证书出错,请登录微信支付商户平台下载证书]]></return_msg>
<result_code><![CDATA[FAIL]]></result_code>
<err_code><![CDATA[CA_ERROR]]></err_code>
<err_code_des><![CDATA[证书出错,请登录微信支付商户平台下载证书]]></err_code_des>
<mch_billno><![CDATA[]]></mch_billno>
<mch_id><![CDATA[]]></mch_id>
<wxappid><![CDATA[]]></wxappid>
<re_openid><![CDATA[]]></re_openid>
<total_amount>100</total_amount>
</xml>