以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [讨论]大神能否转一下C#微信红包的源码为狐表可用 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=113876) |
-- 作者:zwjfx -- 发布时间:2018/1/24 15:06:00 -- [讨论]大神能否转一下C#微信红包的源码为狐表可用 public ActionResult CashRedPack() {
//post请求 string reqUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
string nonce_str = WeChatMPToken.createNonceStr();//随机字符串 string sign = ""; // string mch_billno = ""; //商户订单号 string mch_id = "555555"; //商户号 string wxappid = "yyyyyy"; //公众号appid string nick_name = "oo"; //提供方名称 string send_name = "oo"; //商户名称 string re_openid = "oAeN5js2Jlm7bK851l8sTLSfur64"; //用户openid int total_amount = 100; //付款金额 int min_value = 100; //红包最小的金额 int max_value = 100; //红包最大的金额 int total_num = 1; //红包发放的人数 string wishing = "送您的现金红包"; //红包祝福语 string client_ip = "192.168.0.1"; //ip地址 string act_name = "晒单返现"; //活动名称 string remark = "晒单返现第二波"; // 活动备注 string logo_imgurl = "https://mmbiz.qlogo.cn";// 商户logo的url 选填参数
DateTime now = DateTime.Now;
Random don = new Random(); string str = string.Empty;
for (int i = 0; i < 10; i++) { str += don.Next(0, 10); }
mch_billno = mch_id + now.Year + now.Month + now.Day + str; //计算sign
string stringSignTemp = string.Empty;
stringSignTemp += "act_name=" + act_name; stringSignTemp += "&client_ip=" + client_ip; if (!string.IsNullOrEmpty(logo_imgurl)) { stringSignTemp += "&logo_imgurl=" + logo_imgurl;
}
stringSignTemp += "&max_value=" + max_value; stringSignTemp += "&mch_billno=" + mch_billno; stringSignTemp += "&mch_id=" + mch_id; stringSignTemp += "&min_value=" + min_value; stringSignTemp += "&nick_name=" + nick_name; stringSignTemp += "&n="" auto;"=""> stringSignTemp += "&re_openid=" + re_openid; stringSignTemp += "&remark=" + remark; stringSignTemp += "&send_name=" + send_name; stringSignTemp += "&total_amount=" + total_amount; stringSignTemp += "&total_num=" + total_num; stringSignTemp += "&wishing=" + wishing; stringSignTemp += "&wxappid=" + wxappid; stringSignTemp += "&key=" + "199Mimo3848zhrmdgje33djgifekdo9"; //这个是你的api密钥
//计算MD5值 sign = MD5Security.GetMD5(stringSignTemp).ToUpper();
string fuckyou = string.Empty; fuckyou = "<xml>"; fuckyou += "<act_name><![CDATA[" + act_name + "]]></act_name>"; fuckyou += "<client_ip><![CDATA[" + client_ip + "]]></client_ip>"; fuckyou += "<logo_imgurl><![CDATA[" + logo_imgurl + "]]></logo_imgurl>"; fuckyou += "<max_value><![CDATA[" + max_value + "]]></max_value>"; fuckyou += "<mch_billno><![CDATA[" + mch_billno + "]]></mch_billno>"; fuckyou += "<mch_id><![CDATA[" + mch_id + "]]></mch_id>"; fuckyou += "<min_value><![CDATA[" + min_value + "]]></min_value>"; fuckyou += "<nick_name><![CDATA[" + nick_name + "]]></nick_name>"; fuckyou += "<nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>"; fuckyou += "<re_openid><![CDATA[" + re_openid + "]]></re_openid>"; fuckyou += "<remark><![CDATA[" + remark + "]]></remark>"; fuckyou += "<send_name><![CDATA[" + send_name + "]]></send_name>"; fuckyou += "<total_amount><![CDATA[" + total_amount + "]]></total_amount>"; fuckyou += "<total_num><![CDATA[" + total_num + "]]></total_num>"; fuckyou += "<wishing><![CDATA[" + wishing + "]]></wishing>"; fuckyou += "<wxappid><![CDATA[" + wxappid + "]]></wxappid>"; fuckyou += "<sign><![CDATA[" + sign + "]]></sign>"; fuckyou += "</xml>"; //System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Parse(fuckyou);
//Stream xmlStream = new MemoryStream(); //xml.Save(xmlStream); //byte[] bytes = new byte[xmlStream.Length]; //xmlStream.Read(bytes, 0, bytes.Length); //// 设置当前流的位置为流的开始 //xmlStream.Seek(0, SeekOrigin.Begin);
Encoding encoding = Encoding.UTF8; byte[] bytes = encoding.GetBytes(fuckyou);
//发送post 请求 附带折证书
try { //string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; string cert = @"C:\\cert\\apiclient_cert.p12"; string password = "商户号";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(cert, password,X509KeyStorageFlags.MachineKeySet); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(reqUrl); 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(); } } catch (Exception exp) {
}
//解析xml return Json(new { isSuccess = 1 },JsonRequestBehavior.AllowGet);
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; return false; } [此贴子已经被作者于2018/1/24 15:07:41编辑过]
|
-- 作者:有点甜 -- 发布时间:2018/1/24 15:41:00 -- 如果想转,参考
|
-- 作者:zwjfx -- 发布时间:2018/1/25 21:06:00 -- 请问:post提交数据的时候,要怎么同时提交证书? Dim hc As New HttpClient(reqUrl) hc.Timeout = 5 \'设置超时为5秒 hc.Content = jo.Tostring() Dim ret As String = hc.GetData() MessageBox.show(ret) 这里应该怎么同时提交证书 //发送post 请求 附带证书 try { //string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; string cert = @"C:\\cert\\apiclient_cert.p12"; string password = "商户号"; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(cert, password,X509KeyStorageFlags.MachineKeySet); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(reqUrl); 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(); } } catch (Exception exp) { } 这部分研究了好久没搞定,烦请指教,感谢
|
-- 作者:有点甜 -- 发布时间:2018/1/25 21:50:00 -- 1、全局代码
Public Function CheckValidationResult(sender As Object, certificate As System.Security.Cryptography.X509Certificates.X509Certificate, chain As System.Security.Cryptography.X509Certificates.X509Chain, errors As System.Net.Security.SslPolicyErrors) As Boolean
2、参考
dim bytes Dim cert As String = "C:\\cert\\apiclient_cert.p12" Dim cer As System.Security.Cryptography.X509Certificates.X509Certificate = New System.Security.Cryptography.X509Certificates.X509Certificate(cert, password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.MachineKeySet) webrequest.ContentLength = bytes.Length |
-- 作者:zwjfx -- 发布时间:2018/1/25 22:24:00 -- 感谢有点甜 |