Foxtable(狐表)用户栏目专家坐堂 → [讨论]大神能否转一下C#微信红包的源码为狐表可用


  共有3035人关注过本帖树形打印复制链接

主题:[讨论]大神能否转一下C#微信红包的源码为狐表可用

帅哥哟,离线,有人找我吗?
zwjfx
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:146 积分:2622 威望:0 精华:0 注册:2014/3/18 12:20:00
[讨论]大神能否转一下C#微信红包的源码为狐表可用  发帖心情 Post By:2018/1/24 15:06:00 [只看该作者]

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 ****you = string.Empty;

           ****you = "<xml>";

           ****you += "<act_name><![CDATA[" + act_name + "]]></act_name>";

           ****you += "<client_ip><![CDATA[" + client_ip + "]]></client_ip>";

           ****you += "<logo_imgurl><![CDATA[" + logo_imgurl + "]]></logo_imgurl>";

           ****you += "<max_value><![CDATA[" + max_value + "]]></max_value>";

           ****you += "<mch_billno><![CDATA[" + mch_billno + "]]></mch_billno>";

           ****you += "<mch_id><![CDATA[" + mch_id + "]]></mch_id>";

           ****you += "<min_value><![CDATA[" + min_value + "]]></min_value>";

           ****you += "<nick_name><![CDATA[" + nick_name + "]]></nick_name>";

           ****you += "<nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>";

           ****you += "<re_openid><![CDATA[" + re_openid + "]]></re_openid>";

           ****you += "<remark><![CDATA[" + remark + "]]></remark>";

           ****you += "<send_name><![CDATA[" + send_name + "]]></send_name>";

           ****you += "<total_amount><![CDATA[" + total_amount + "]]></total_amount>";

           ****you += "<total_num><![CDATA[" + total_num + "]]></total_num>";

           ****you += "<wishing><![CDATA[" + wishing + "]]></wishing>";

           ****you += "<wxappid><![CDATA[" + wxappid + "]]></wxappid>";

           ****you += "<sign><![CDATA[" + sign + "]]></sign>";

           ****you += "</xml>";

           //System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Parse(****you);

 

 

 

           //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(****you);

 

 

           //发送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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/1/24 15:41:00 [只看该作者]

如果想转,参考

 

http://converter.telerik.com/

 


 回到顶部
帅哥哟,离线,有人找我吗?
zwjfx
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:146 积分:2622 威望:0 精华:0 注册:2014/3/18 12:20:00
  发帖心情 Post By: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)
           {
                
           }
这部分研究了好久没搞定,烦请指教,感谢

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By: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
 ' 总是接受   
 Return True
End Function

 

 

2、参考

 

dim bytes
dim requrl = "https://api.mch.weixin.qq.com/secapi/pay/refund"

Dim cert As String = "C:\cert\apiclient_cert.p12"
Dim password As String = "商户号"
System.Net.ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(addressof CheckValidationResult)

Dim cer As System.Security.Cryptography.X509Certificates.X509Certificate = New System.Security.Cryptography.X509Certificates.X509Certificate(cert, password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.MachineKeySet)
Dim webrequest As net.HttpWebRequest = net.HttpWebRequest.Create(reqUrl)
webrequest.ClientCertificates.Add(cer)
webrequest.Method = "post"

webrequest.ContentLength = bytes.Length
webrequest.GetRequestStream().Write(bytes, 0, bytes.Length)
Dim webreponse As Net.httpWebResponse = webrequest.GetResponse()
Dim stream As io.Stream = webreponse.GetResponseStream()
Dim resp As String = String.Empty
Using reader As io.StreamReader = New io.StreamReader(stream)
resp = reader.ReadToEnd()
End Using


 回到顶部
帅哥哟,离线,有人找我吗?
zwjfx
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:146 积分:2622 威望:0 精华:0 注册:2014/3/18 12:20:00
  发帖心情 Post By:2018/1/25 22:24:00 [只看该作者]

感谢有点甜

 回到顶部