欢迎光临,了解微信小程序开发,就上易用通!

微信小程序 带参数二维码 C# asp.net 服务端程序

发布:2018-04-17 12:13浏览: 来源:网络 作者:cola

作者:采购员乙,来自原文地址 
第一步 获取access_token:

文档如下:

http请求方式: GET 
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 
代码如下:

 

				
  1. string result = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx***********d&secret=a*******************4");
  2. public static string HttpGet(string Url)
  3. {
  4. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  5. request.Method = "GET";
  6. request.ContentType = "text/html;charset=UTF-8";
  7.  
  8.  
  9. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  10. Stream myResponseStream = response.GetResponseStream();
  11. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  12. string retString = myStreamReader.ReadToEnd();
  13. myStreamReader.Close();
  14. myResponseStream.Close();
  15.  
  16.  
  17.  
  18. return retString;
  19. }

其中****改成自己的。具体到微信公众平台小程序里设置开发设置里找。

第二步 获取推广二维码

文档:

https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN  POST 参数说明

参数 默认值 说明  path 不能为空,最大长度 128 字节  width 430 二维码的宽度 代码:

 

				
  1. public static void PostMoths(string access_token)
  2. {
  3. string _url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + access_token;
  4.  
  5.  
  6. string strURL = _url;
  7. System.NET.HttpWebRequest request;
  8. request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
  9. request.Method = "POST";
  10. request.ContentType = "application/json;charset=UTF-8";
  11. JsonData _data = new JsonData();
  12. _data["path"] = "pages/index?query=1";
  13. _data["width"] = "430";
  14.  
  15.  
  16. string _jso = _data.ToJson();
  17. //string paraUrlCoded = param;
  18. byte[] payload;
  19. //payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  20. payload = System.Text.Encoding.UTF8.GetBytes(_jso);
  21. request.ContentLength = payload.Length;
  22. Stream writer = request.GetRequestStream();
  23. writer.Write(payload, 0, payload.Length);
  24. writer.Close();
  25. System.Net.HttpWebResponse response;
  26. response = (System.Net.HttpWebResponse)request.GetResponse();
  27. System.IO.Stream s;
  28. s = response.GetResponseStream();
  29. string StrDate = "";
  30. string strValue = "";
  31. byte[] tt = StreamToBytes(s);
  32. //将流保存在c盘test.png文件下
  33. System.IO.File.WriteAllBytes(@"d:\test.png", tt);
  34. }
  35. ///将数据流转为byte[]
  36. public static byte[] StreamToBytes(Stream stream)
  37. {
  38. List<byte> bytes = new List<byte>();
  39. int temp = stream.ReadByte();
  40. while (temp != -1)
  41. {
  42. bytes.Add((byte)temp);
  43. temp = stream.ReadByte();
  44. }
  45. return bytes.ToArray();
  46. }

最后保存在d盘的图片就是推广二维码,可以讲服务器连接地址发给微信小程序,供微信小程序调用。






免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。