小程序带参数二维码生成小程序带参数二维码生成 c#后台代码后台代码
微信小程序获取小程序页面二维码API接口,通过后台post网址https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?
access_token=ACCESS_TOKEN,
获取access_token方式,参数{"path": "pages/index/index?query=1", "width": 260},可以获得图片二进制流,直接打印二进制
流为乱码
(此图片来源于网络,如有侵权,请联系删除! )
该二进制流直接保存成png图片,一下为本人所写的部分核心代码,需要引用using system.net
/// url 网址 || param 参数
1. public static string PostMoths(string url, string param)
2. {
3. string strURL = url;
4. System.Net.HttpWebRequest request;
5. request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
6. request.Method = "POST";
7. request.ContentType = "application/json;charset=UTF-8";
8. string paraUrlCoded = param;
9. byte[] payload;
10. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
11. request.ContentLength = payload.Length;
12. Stream writer = request.GetRequestStream();
13. writer.Write(payload, 0, payload.Length);
14. writer.Close();
15. System.Net.HttpWebResponse response;
16. response = (System.Net.HttpWebResponse)request.GetResponse();
17. System.IO.Stream s;
18. s = response.GetResponseStream();
19. string StrDate = "";
20. string strValue = "";
21. byte[] tt = StreamToBytes(s);
22. //将流保存在c盘test.png文件下
23. System.IO.File.WriteAllBytes(@"c: est.png", tt);
24. }
25. ///将数据流转为byte[]
26. public static byte[] StreamToBytes(Stream stream)
27. {
28. List bytes = new List();
29. int temp = stream.ReadByte();
30. while (temp != -1)
31. {
32. bytes.Add((byte)temp);
33. temp = stream.ReadByte();
34. }
评论0