c# 微信发普通红包报证书错误

qtvb1987 2017-04-06 08:01:21

服务器已在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>
...全文
277 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qtvb1987 2017-04-20
  • 打赏
  • 举报
回复
引用 1 楼 江南小鱼的回复:
服务器证书的问题,确定在商户平台下载的证书导入到服务器了?
找到问题了,下错了商户号的证书
江南小鱼 2017-04-07
  • 打赏
  • 举报
回复
服务器证书的问题,确定在商户平台下载的证书导入到服务器了?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧