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();
}
原文地址:https://www.cnblogs.com/smileZeng/p/11725575.html
时间: 2024-10-14 19:07:44