发布:2022/3/26 17:48:23作者:管理员 来源:本站 浏览次数:1123
1、生成微信小程序二维码,需要获取微信小程序Access_Token。代码如下,先获取Access_Token
/// <summary>
/// 获取微信小程序Access_Token
/// </summary>
/// <param name="appId">小程序唯一凭证</param>
/// <param name="appSecret">小程序唯一凭证密钥</param>
/// <returns></returns>
public static string GetAccessToken(string appId, string appSecret)
{
string access_token = string.Empty;
try
{
string token_url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(token_url);
//请求方式
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();
XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
xmlReader.Read();
string xml = xmlReader.ReadOuterXml();
myResponse.Close();
stream.Close();
stream.Dispose();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement rootElement = doc.DocumentElement;
access_token = rootElement.SelectSingleNode("access_token").InnerText.Trim();
return access_token;
}
catch (Exception ex)
{
throw ex;
}
}
2、生成微信小程序二维码,无限次数
/// <summary>
/// 生成微信小程序二维码
/// </summary>
/// <param name="picPath"></param>
/// <param name="access_token"></param>
/// <param name="paramData"></param>
/// <param name="page"></param>
/// <param name="width"></param>
public static void GetWeChatCodeLimit(string picPath, string access_token, string paramData = null, string page = null, int width = 430)
{
try
{
//这个路径生成的二维码是无限次数,scene参数最大32位
string url = $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={access_token}";
Dictionary<string, object> dic = new Dictionary<string, object>();
if (string.IsNullOrWhiteSpace(paramData))
{
paramData = "id=123";
}
dic.Add("scene", paramData); //必填,有不能为空,随便填好了
if (!string.IsNullOrWhiteSpace(page))
{
dic.Add("path", page); //注意这里要用 path,而不是用page,官网有问题
}
dic.Add("width", width);
string json = JsonHelper.GetJSON<Dictionary<string, object>>(dic);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/json;charset=UTF-8";
byte[] payload = Encoding.UTF8.GetBytes(json);
myRequest.ContentLength = payload.Length;
Stream writer = myRequest.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();//返回图片数据流
byte[] byteArray = StreamToBytes(stream);//将数据流转为byte[]
if (byteArray.Length > 1000)
{
File.WriteAllBytes(picPath, byteArray);
}
else
{
//转字符串--报错
string str = Encoding.Default.GetString(byteArray);
throw new Exception(str);
}
myResponse.Close();
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
3、生成微信小程序二维码,有限次数
/// <summary>
/// 生成微信小程序二维码
/// </summary>
/// <param name="picPath"></param>
/// <param name="access_token"></param>
/// <param name="paramData"></param>
/// <param name="page"></param>
/// <param name="width"></param>
public static void CreateWeChatQrCode(string picPath, string access_token, string paramData = null, string page = null, int width = 430)
{
try
{
//这个路径有限次数,1000,000个,要注意使用
//string url = $"https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={access_token}";
//这个生成是小程序码,上面是二维码
string url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={access_token}";
if (!string.IsNullOrWhiteSpace(paramData))
{
page += "?" + paramData;
}
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("path", page); //必填
dic.Add("width", width);
string json = JsonHelper.GetJSON<Dictionary<string, object>>(dic);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/json;charset=UTF-8";
byte[] payload = Encoding.UTF8.GetBytes(json);
myRequest.ContentLength = payload.Length;
Stream writer = myRequest.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();//返回图片数据流
byte[] byteArray = StreamToBytes(stream);//将数据流转为byte[]
if (byteArray.Length > 1000)
{
File.WriteAllBytes(picPath, byteArray);
}
else
{
//转字符串
string str = Encoding.Default.GetString(byteArray);
throw new Exception(str);
}
myResponse.Close();
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将数据流转为byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public 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();
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4