小程序生成小程序码
Posted smilezeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小程序生成小程序码相关的知识,希望对你有一定的参考价值。
getUnlimited 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
小程序官方文档 wxacode.getUnlimited
/// <summary>
/// 获取小店小程序码
/// </summary>
/// <returns>返回的图片 Buffer</returns>
public object GetWxacode(string pagePath, int width, string Id)
{
var accessToken = "";//获取小程序全局唯一后台接口调用凭据(access_token
)
var data = "{"scene":"" + Id + "","path":"" + HttpUtility.UrlDecode(pagePath) + "","width":" + width + "}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); //创建url
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
byte[] load = Encoding.UTF8.GetBytes(data);
request.ContentLength = load.Length;
Stream writer = request.GetRequestStream();
writer.Write(load, 0, load.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
byte[] mg = StreamToBytes(s);
MemoryStream ms = new MemoryStream(mg);
string qrCodeImagePath = string.Empty;
qrCodeImagePath = "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());
ms.Dispose();
var QrCodeImagePath = qrCodeImagePath;
return QrCodeImagePath;
}
private static byte[] StreamToBytes(Stream stream)
{
List<byte> bytes = new List<byte>();
int temp = stream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = stream.ReadByte();
}
return bytes.ToArray();
}
以上是关于小程序生成小程序码的主要内容,如果未能解决你的问题,请参考以下文章