微信支付退款接口,始终无法解法“未能创建 SSL/TLS 安全通道”的问题

piao2004 2017-08-27 12:06:22
提交代码:

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
//网上查到的所有资料,都说加上下面这一句就可以,但是我不仅加了,还把SecurityProtocolType的枚举一个个试了个遍,都不行。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(“https://api.mch.weixin.qq.com/secapi/pay/refund”);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf8";
req.ContentLength = postBytes.Length;

string CertPath = HttpContext.Current.Request.PhysicalApplicationPath + "\\wx\\cert\\apiclient_cert.p12";
X509Certificate2 cert = new X509Certificate2(CertPath, WxPayAccount);
req.ClientCertificates.Add(cert);

//就这下面这句GetRequestStream时出错:出错信息:请求被中止: 未能创建 SSL/TLS 安全通道。
Stream reqStream = req.GetRequestStream();
reqStream.Write(postBytes, 0, postBytes.Length);
reqStream.Close();

WebResponse webRep = req.GetResponse();
StreamReader sr = new StreamReader(webRep.GetResponseStream(), System.Text.Encoding.UTF8);
RMsg = sr.ReadToEnd();



注:从微信支付平台下载的证书已在服务器上安装。并且在浏览直接访问https://api.mch.weixin.qq.com/secapi/pay/refund时,是可以的:

(如果没有安装证书,浏览器访问微信退款接口是打不开的)

.net Framework: v4.5
服务器操作系统:windows server 2016
IIS10

===========
注:我以前为其它客户做过开发,是.net4.0,windows server 2008,根本不用加SecurityProtocol 那一句,都可以正常提交呢。
...全文
1010 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
键盘敲出字 2017-08-28
  • 打赏
  • 举报
回复
虽然不懂,但是为什么
 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(“https://api.mch.weixin.qq.com/secapi/pay/refund”);
的引号是中文的。
piao2004 2017-08-28
  • 打赏
  • 举报
回复
引用 2 楼 wowfiowow 的回复:
用苏飞那个模块试试?
什么苏飞模块?谢谢。
piao2004 2017-08-27
  • 打赏
  • 举报
回复
补充:已确保证书路径正确无误
橘子皮... 2017-08-27
  • 打赏
  • 举报
回复
用苏飞那个模块试试?

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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