微信退款证书使用c#

时间:2018-08-31 18:02:25   收藏:0   阅读:561
原文:微信退款证书使用c#

微信退款需要证书

data为已封装好的xml数据

具体怎么封装>打开

 1     public string get(string data) 
 2        {
 3             string cert = @"D:\certificate\apiclient_cert.p12"; //证书位置
 4             string password = "11100011";//证书密码
 5             string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";//请求地址
 6             ServicePointManager.ServerCertificateValidationCallback=new 
 7             RemoteCertificateValidationCallback(CheckValidationResult); 
 8             X509Certificate cer = new X509Certificate(cert, password); 
 9             HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); 
10             webrequest.ClientCertificates.Add(cer);
11             byte[] bs = Encoding.UTF8.GetBytes(data);
12 
13             webrequest.Method = "POST";
14             webrequest.ContentType = "application/x-www-form-urlencoded";
15             webrequest.ContentLength = bs.Length;
16             //提交请求数据
17             Stream reqStream = webrequest.GetRequestStream();
18             reqStream.Write(bs, 0, bs.Length);
19             reqStream.Close();
20             //接收返回的页面,必须的,不能省略
21             WebResponse wr = webrequest.GetResponse();
22             System.IO.Stream respStream = wr.GetResponseStream();
23             System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
24             string t = reader.ReadToEnd();
25             System.Web.HttpContext.Current.Response.Write(t);
26             wr.Close();
27 
28             return t;
29             
30 
31 
32             }
33 
34         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
35         {
36             if (errors == SslPolicyErrors.None)
37                 return true;
38             return false;
39         }

 

 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!