小程序码带参请求(邀请码)
作者:互联网
#region 生成小程序推荐码
/// <summary>
/// 微信小程序带参数二维码的生成
/// </summary>
/// <param name="ParentIDs">邀请人员ID</param>
/// <returns></returns>
public static string CreateWxCode(string ParentIDs)
{
string access_token = string.Empty;
string url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url1);
webrequest.Method = "GET";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
StreamReader reader = new StreamReader(webresponse.GetResponseStream());
access_token = reader.ReadToEnd();
JObject jData = (JObject)JsonConvert.DeserializeObject(access_token);
access_token = jData["access_token"].ToString();
string ret = string.Empty;
try
{
string DataJson = string.Empty;
//适用于需要的码数量极多,或仅临时使用的业务场景
//通过该接口生成的小程序码,永久有效,数量暂无限制。
string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
//DataJson的配置见小程序开发文档,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
ret = CreateWeChatQrCode(url, ParentIDs);
}
catch (Exception e)
{
ret = e.Message;
}
return ret;//返回图片地址
}
/// <summary>
/// 返回二维码图片
/// </summary>
/// <param name="url"></param>
/// <param name="ParentIDs"></param>
/// <returns></returns>
public static string CreateWeChatQrCode(string url, string ParentIDs)
{
HttpWebRequest request;
string imgName = string.Empty;
string path = string.Empty;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = "{\"scene\": \"" + ParentIDs + "\"}";
//byte[] payload;
byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();//返回图片数据流
byte[] tt = StreamToBytes(s);//将数据流转为byte[]
imgName = "user_" + ParentIDs + ".png";
path = "/upload/ewm/";
//获取相对于应用的基目录创建目录
//string imgPath = System.Web.Hosting.HostingEnvironment.MapPath(path);//System.AppDomain.CurrentDomain.baxxxxseDirectory + path; //通过此对象获取文件名
string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path; //通过此对象获取文件名
if (!Directory.Exists(imgPath))
{
Directory.CreateDirectory(imgPath);
}
System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + imgName), tt);//讲byte[]存储为图片
}
catch (Exception ex)
{
Common.Log("CreateWeChatQrCode:" + ex.Message);
}
return path + imgName;
}
/// <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();
}
#endregion
//写法比较随意。提供参考思路
标签:请求,码带,System,access,token,邀请,path,byte,string 来源: https://blog.csdn.net/Freya0222/article/details/117667010