欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信退款证书使用c#

程序员文章站 2022-12-25 16:32:03
微信退款需要证书 data为已封装好的xml数据 具体怎么封装>打开 ......

微信退款需要证书

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         }