.net微信服务号发送红包
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要为大家详细介绍了.微信服务号发送红包的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了.微信红包发送代码,供大家参考,具体内容如下
注:需要开通微信支付的服务号!
//跳转微信登录页面 public ActionResult Index() { ViewBag.url = "https://open.weixin.qq./connect/oauth2/authorize?appid=" + {服务号appid} + "&redirect_uri=http%3A%2F%2F" + {微信重定向域名(填写程序域名,例如:.xxxx.)} + "%2F"+{程序控制器名,例如:Home}+"%2F"+{程序Action名,例如:RedirectWeChat}+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; return View(); } //获取aesstoken(访问微信接口需要) public static string aesstoken(string WeChatWxAppId, string WeChatWxAppSecret) { string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq./cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", WeChatWxAppId, WeChatWxAppSecret)); if (strJson.IndexOf("errcode") == -1) { return GetJsonValue(strJson, "aess_token"); } else { return ""; } } //解析json public static string GetJsonValue(string jsonStr, string key) { string result = string.Empty; if (!string.IsNullOrEmpty(jsonStr)) { key = "\"" + key.Trim('"') + "\""; int index = jsonStr.IndexOf(key) + key.Length + 1; if (index > key.Length + 1) { //先截逗号,若是一个,截“}”号,取最小值 int end = jsonStr.IndexOf(',', index); if (end == -1) { end = jsonStr.IndexOf('}', index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格 } } return result; } //请求url public static string RequestUrl(string url, string method="post") { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.ContentType = "text/html"; request.Headers.Add("charset", "utf-8"); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } //接收微信返回code //接收微信数据获取用户信息 public ActionResult RedirectWeChat(string code, string state) { if (string.IsNullOrEmpty(code)) { return Content("您拒绝了授权!"); } string aess_token = aesstoken(微信AppId, 微信AppSecret); string st = "https://api.weixin.qq./sns/oauth2/aess_token?appid=" + 微信AppId + "&secret=" + 微信AppSecret + "&code=" + code + "&grant_type=authorization_code"; string data = RequestUrl(st); //拿到用户openid string openid=GetJsonValue(data, "openid"); //获取用户其他信息 string url = "https://api.weixin.qq./cgi-bin/user/info?aess_token=" + aess_token + "&openid=" + openid + "&lang=zh_CN"; data = RequestUrl(url); string subscribe=GetJsonValue(data, "subscribe"); if (subscribe == "0") { ///未关注 return RedirectToAction(""); } return RedirectToAction(""); } //发送红包Action public ActionResult HB() { string openid = "";//用户openid string url = "https://api.mch.weixin.qq./mmpaymkttransfers/sendredpack"; string orderNo = 商户号 + DateTime.Now.ToString("yyyymmdd")+"随机10位数字";//商户订单号 组成mch_id+yyyymmdd+10位一天内不能重复的数字。 string Code = ""//32为随机字符串; string key="key=" + "";//支付密钥(在商户平台设置32为字符串) Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("act_name", "");//活动名称 data.Add("client_ip", "192.168.1.1");//Ip地址 data.Add("mch_billno", orderNo);//商户订单号 组成mch_id+yyyymmdd+10位一天内不能重复的数字。 data.Add("mch_id", "");//商户号 data.Add("nonce_str", Code);//随机字符串 data.Add("re_openid", openid);//用户openid data.Add("remark", "");//备注 data.Add("send_name","");//商户名称 data.Add("total_amount", "100");//付款金额 单位分 data.Add("total_num", "1");//红包发放总人数 data.Add("wishing", "恭喜发财");//红包祝福语 data.Add("wxappid", );//公众账号appid string xml = GetXML(data, key);//签名+拼接xml string str=PostWebRequests(url, xml);//微信返回xml err_code=SUCCESS 就是成功 return View(""); } //发送红包(MD5签名+拼接XML) public static string GetXML(Dictionary<string, string> data,string paykey) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); var data1=from d in data orderby d.Key select d; string data2 = ""; string XML = "<xml>"; foreach (var item in data1) { //空值不参与签名 if (item.Value + "" != "") { data2 += item.Key +"="+ item.Value + "&"; } XML += "<" + item.Key + ">" + item.Value+""+ "</" + item.Key + ">"; } data2 += paykey; //创建md5对象 byte[] inputBye; byte[] outputBye; //使用GB2312编码方式把字符串转化为字节数组. try { inputBye = Encoding.UTF8.GetBytes(data2); } catch { inputBye = Encoding.GetEncoding("GB2312").GetBytes(data2); } outputBye = m5.ComputeHash(inputBye); retStr = System.BitConverter.ToString(outputBye); retStr = retStr.Replace("-", "").ToUpper(); XML += "<sign>" + retStr + "</sign>";//签名 XML += "</xml>"; return XML; } //发送红包请求Post方法 public static string PostWebRequests(string postUrl, string menuInfo) { string returnValue = string.Empty; try { Encoding encoding = Encoding.UTF8; byte[] bytes = encoding.GetBytes(menuInfo); string cert = @"E:\cdcert\apiclient_cert.p12";//支付证书路径 string password = "1212121";//支付证书密码 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); X509Certificate cer = new X509Certificate(cert, password, X509KeyStorageFlags.MachineKeySet); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(postUrl); 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)) { return reader.ReadToEnd(); } } catch (Exception ex) { return ""; } }
以下是微信开发官方相关文档
1.
2.
3.
4.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:.NET微信公众号查看关注者接口
下一篇:.Net项目中一些常用验证操作
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程