In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to use .net to build Wechat service account to send red envelopes. The article is rich and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Note: you need to open WeChat Pay's service number!
/ / Redirect Wechat login page public ActionResult Index () {ViewBag.url = "https://open.weixin.qq.com/connect/oauth3/authorize?appid=" + {service number appid} +" & redirect_uri=http%3A%2F%2F "+ {Wechat redirect domain name (fill in the program domain name, for example: www.xxxx.com)} +"% 2F "+ {program controller name, for example: Home} +"% 2F "+ {program Action name For example: RedirectWeChat} + "& response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" Return View ();} / / get accesstoken (required to access Wechat API) 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" }} / / parse 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) {/ / truncated comma, if the last, truncate "}" sign, take the minimum value int end = jsonStr.IndexOf (',', index) If (end = =-1) {end = jsonStr.IndexOf ('}', index);} result = jsonStr.Substring (index, end-index); result = result.Trim (new char [] {'",'','\'}); / / filter quotes or spaces}} return result;} / / request urlpublic static string RequestUrl (string url, string method=" post ") {/ / set parameter 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"); / / send the request and get the corresponding response data HttpWebResponse response = request.GetResponse () as HttpWebResponse; / / until the request.GetResponse () program starts sending the Post request Stream responseStream = response.GetResponseStream (); StreamReader sr = new StreamReader (responseStream, Encoding.UTF8) / / return the result web page (html) code string content = sr.ReadToEnd (); return content;} / receive Wechat return code// receive Wechat data to get user information public ActionResult RedirectWeChat (string code, string state) {if (string.IsNullOrEmpty (code)) {return Content ("you refused authorization!") ;} string access_token = accesstoken (Wechat AppId, Wechat AppSecret); string st = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=" + Wechat AppId +" & secret= "+ Wechat AppSecret +" & code= "+ code +" & grant_type=authorization_code "; string data = RequestUrl (st); / / get user openidstring openid=GetJsonValue (data," openid ") / / get other user information 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 ") {/ / not following return RedirectToAction (");} return RedirectToAction (");} / send red packet Actionpublic ActionResult HB () {string openid=" / / user openid string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; string orderNo = merchant number + DateTime.Now.ToString (" yyyymmdd ") +" random 10 digits "; / / merchant order number composition: mch_id+yyyymmdd+10 digits that cannot be repeated within a day. String Code = "" / / 32 is a random string; string key= "key=" + ""; / / payment key (set 32 as a string on merchant platform) Dictionary data = new Dictionary (); data.Add ("act_name", ""); / / activity name data.Add ("client_ip", "192.168.1.1"); / / Ip address data.Add ("mch_billno", orderNo) / / Merchant order number composition: mch_id+yyyymmdd+10 digits that cannot be repeated within a day. Data.Add ("mch_id", "); / merchant number data.Add (" nonce_str ", Code); / / random string data.Add (" re_openid ", openid); / / user openid data.Add (" remark ","); / / remarks data.Add (" send_name ","); / / merchant name data.Add (" total_amount "," 100 ") / / the unit of payment amount is data.Add ("total_num", "1"); / / the total number of red packet payers data.Add ("wishing", "Kung Xi Fat Choi"); / / Red packet blessing message data.Add ("wxappid",); / / Public account appid string xml = GetXML (data, key); / / signature + stitching xml string str=PostWebRequests (url, xml); / / successful return View (") when Wechat returns xml err_code=SUCCESS ("). } / / send red packets (MD5 signature + stitching XML) public static string GetXML (Dictionary data,string paykey) {string retStr; MD5CryptoServiceProvider M5 = new MD5CryptoServiceProvider (); var data1=from d in data orderby d.Key select d; string data2 = ""; string XML = ""; foreach (var item in data1) {/ / null values do not participate in signing if (item.Value + "! =") {data2 + = item.Key + "=" + item.Value + "&" } XML + = "" + item.Value+ "" + ";} data2 + = paykey; / / create md5 object byte [] inputBye; byte [] outputBye; / / convert strings to byte arrays using GB2312 encoding. 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 + =" + retStr + "; / / sign XML + ="; return XML;} / / send red packet request Post method 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 "; / / payment certificate path string password =" 1212121 " / / payment certificate password 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 ";}} above is what the editor has shared about how to use .net to build Wechat service account to send red packets. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.