//scene_id 参数
public string GetewmPic(string scene_id)
{
JavaScriptSerializer js = new JavaScriptSerializer();
//Redis获取token
RedisHelper rds = new RedisHelper();
string access_token_key = "access_token_gzh";
string access_token = rds.ReadStr(access_token_key, 12);
if (string.IsNullOrEmpty(access_token))
{
//Redis没有从公众号获取
string url2 = "https://api.weixin.qq.com/cgi-bin/token";
string json = StrHelper.GetRequestUrl(url2,"grant_type=client_credential&appid=appid&secret=secret");
access_token_model lst = JsonConvert.DeserializeObject<access_token_model>(json);
access_token = lst.access_token;
rds.InsertStr(access_token_key, access_token, System.DateTime.Now.AddMinutes(5).ToUniversalTime(), 12);
}
string tocket = "";
string poster = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + scene_id + "\"}}}";
string tockes = StrHelper.PostRequestUrl("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + access_token, poster);
action_info wUser = js.Deserialize<action_info>(tockes);
tocket = Uri.EscapeDataString(wUser.ticket); //TICKET必需UrlEncode
string Picurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + tocket;
//将二维码下载到本地
string allurl = StrHelper.PostMothswxewm(Picurl, "");
return allurl;
}
public static string PostMothswxewm(string url, string param)
{
string strURL = url;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
string paraUrlCoded = param;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();//返回图片数据流
var res = ReadQRCode(s);
var Bitmap = CreateQRCode(res, 104, 104);
var stream = new System.IO.MemoryStream();
Bitmap.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
string date = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
string filePath = "xx/sceneimg/" + date + ".jpg";
webPost.webPost1("xx/sceneimg/", date + ".jpg", stream);
string allurl = StrHelper.GetPhoto(filePath);
return allurl;
}
标签:scene,string,微信,request,token,System,access,二维码,参数
From: https://blog.csdn.net/weixin_45095882/article/details/144131134