我们在对接微信商户平台的现金红包高级接口时向
https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
接口地址post数据后 返回的xml文件中报 CA证书出错
详细返回XML包: 10017876100
原因是向微信服务器发送请求数据时没有带证书请求:
///
/// 发送请求
///
/// 发送拼接的参数
/// 要发送到的链接地址
/// 返回xml
public static string Send(string data, string url)
{
return Send(Encoding.GetEncoding("UTF-8").GetBytes(data), url);
}
public static string Send(byte[] data, string url)
{
string cert = ConfigurationManager.AppSettings["CertPath"].Trim();//证书存放的地址
string password = ConfigurationManager.AppSettings["MchId"].Trim();//证书密码 即商户号
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert, password);
#region 该部分是关键,若没有该部分则在IIS下会报 CA证书出错
X509Certificate2 certificate = new X509Certificate2(cert, password);
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate); //可省略
store.Add(certificate);
store.Close();
#endregion Stream responseStream; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request == null) { throw new ApplicationException(string.Format("Invalid url string: {0}", url)); } request.UserAgent = sUserAgent; request.ContentType = sContentType; request.ClientCertificates.Add(cer); request.Method = "POST"; request.ContentLength = data.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Close(); try { responseStream = request.GetResponse().GetResponseStream(); } catch (Exception exception) { throw exception; } string str = string.Empty; using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"))) { str = reader.ReadToEnd(); } responseStream.Close(); return str; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; return false; } }