微信退款接口需要添加证书, 特别注意:有些情况下即使在post请求时添加了证书依然报同样的错误,在添加证书时注意,
如果使用: X509Certificate cer = new X509Certificate(path, configInfo.mch_id); 也报错的话
改用:X509Certificate2 cer = new X509Certificate2(path, configInfo.mch_id);
private string requestWxRefundApi(WeixinPromotionChannelConfigInfo configInfo, string param)
{
string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"bin\" + configInfo.cert_path;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate2 cer = new X509Certificate2(path, configInfo.mch_id);
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(configInfo.refund_url);
webrequest.ClientCertificates.Add(cer);
webrequest.Method = "post";
//webrequest.KeepAlive = true;
//webrequest.Timeout = 60 * 1000;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(param);
using (Stream inputStream = webrequest.GetRequestStream())
{
inputStream.Write(data, 0, data.Length);
}
string resp = string.Empty;
using (HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse())
{
Stream stream = webreponse.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
resp = reader.ReadToEnd();
}
}
return resp;
}