麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

.net微信服務號發(fā)送紅包

2024-07-10 13:31:28
字體:
供稿:網(wǎng)友

本文實例為大家分享了.net微信紅包發(fā)送代碼,供大家參考,具體內(nèi)容如下

注:需要開通微信支付的服務號!

//跳轉(zhuǎn)微信登錄頁面public ActionResult Index(){ ViewBag.url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + {服務號appid} + "&redirect_uri=http%3A%2F%2F" + {微信重定向域名(填寫程序域名,例如:www.xxxx.com)} + "%2F"+{程序控制器名,例如:Home}+"%2F"+{程序Action名,例如:RedirectWeChat}+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; return View();}//獲取accesstoken(訪問微信接口需要)public static string accesstoken(string WeChatWxAppId, string WeChatWxAppSecret){ string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", WeChatWxAppId, WeChatWxAppSecret)); if (strJson.IndexOf("errcode") == -1) {  return GetJsonValue(strJson, "access_token"); } else {  return ""; }}//解析jsonpublic 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;}//請求urlpublic static string RequestUrl(string url, string method="post"){ // 設置參數(shù) 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"); //發(fā)送請求并獲取相應回應數(shù)據(jù) HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開始向目標網(wǎng)頁發(fā)送Post請求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); //返回結果網(wǎng)頁(html)代碼 string content = sr.ReadToEnd(); return content;}//接收微信返回code//接收微信數(shù)據(jù)獲取用戶信息public ActionResult RedirectWeChat(string code, string state){ if (string.IsNullOrEmpty(code)) {  return Content("您拒絕了授權!"); } string access_token = accesstoken(微信AppId, 微信AppSecret); string st = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + 微信AppId + "&secret=" + 微信AppSecret + "&code=" + code + "&grant_type=authorization_code"; string data = RequestUrl(st);//拿到用戶openidstring openid=GetJsonValue(data, "openid");//獲取用戶其他信息 string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"; data = RequestUrl(url);string subscribe=GetJsonValue(data, "subscribe"); if (subscribe == "0") {  ///未關注  return RedirectToAction(""); } return RedirectToAction("");}//發(fā)送紅包Actionpublic ActionResult HB(){ string openid = "";//用戶openid string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";  string orderNo = 商戶號 + DateTime.Now.ToString("yyyymmdd")+"隨機10位數(shù)字";//商戶訂單號 組成:mch_id+yyyymmdd+10位一天內(nèi)不能重復的數(shù)字。  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位一天內(nèi)不能重復的數(shù)字。  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");//紅包發(fā)放總人數(shù)  data.Add("wishing", "恭喜發(fā)財");//紅包祝福語  data.Add("wxappid", );//公眾賬號appid  string xml = GetXML(data, key);//簽名+拼接xml  string str=PostWebRequests(url, xml);//微信返回xml err_code=SUCCESS 就是成功 return View(""); }//發(fā)送紅包(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; //創(chuàng)建md5對象 byte[] inputBye; byte[] outputBye; //使用GB2312編碼方式把字符串轉(zhuǎn)化為字節(jié)數(shù)組. 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;}//發(fā)送紅包請求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 ""; }} 

以下是微信開發(fā)官方相關文檔

1. 【微信支付】公眾號支付開發(fā)者文檔
2. 微信開放平臺
3.企業(yè)號開發(fā)者接口文檔
4.微信公眾平臺開發(fā)者文檔

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: a级毛片免费观看在线播放 日本aaa一级片 | 亚洲特黄 | japan护士性xxxⅹhd | 国产一区二区三区手机在线 | 久久久久一区二区三区 | 精品一区二区久久久久久久网精 | 一区二区三区视频播放 | 午夜精品在线视频 | av电影在线免费 | 极色品影院 | 国产伊人色 | 国产精品久久久在线观看 | jizzjizz中国人少妇中文 | 国产精品美女久久久久久网站 | 天天鲁在线视频免费观看 | 日韩av影片在线观看 | 77成人影院 | 国产精品wwww| 日本欧美一区二区三区视频麻豆 | 亚洲天堂一级片 | 国产91久久久久久 | 性欧美极品xxxx欧美一区二区 | 中国女警察一级毛片视频 | 久草在线视频精品 | 日本a∨精品中文字幕在线 狠狠干精品视频 | 欧美性成人 | 成人毛片网站 | 国产一国产精品一级毛片 | 日韩视频在线不卡 | 日韩视频一区在线 | 成人毛片在线免费看 | 欧美成人做爰高潮片免费视频 | 91精品国产一区二区三区四区在线 | 久久精品探花 | 国产精品欧美久久久久一区二区 | 成人免费福利网站 | 亚洲精品午夜国产va久久成人 | av成人免费观看 | 久久精品视频8 | 一区二区久久电影 | 亚洲电影在线观看高清免费 |