c# 微信生成带参数的二维码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Web;  
  8. using LitJson;  
  9.   
  10. /// <summary>  
  11. /// Ticket 的摘要说明  
  12. /// </summary>  
  13. public class Ticket  
  14. {  
  15.     public Ticket()  
  16.     {  
  17.         //  
  18.         // TODO: 在此处添加构造函数逻辑  
  19.         //  
  20.     }  
  21.     public static  string GetewmPic( string scene_id)  
  22.     {  
  23.         string tocket = "";  
  24.         string pic="";  
  25.         if (Config.GetInstance().SystemSetting.Appid!=""&&Config.GetInstance().SystemSetting.seconid!="")  
  26.         {  
  27.   
  28.             string appid = Config.GetInstance().SystemSetting.Appid;// "wx5955bd71e310a7b5";  
  29.             string secret = Config.GetInstance().SystemSetting.seconid;// "2d6b6ec95cde8bc2908713bde4c92434";  
  30.             string parm = "grant_type=client_credential&appid=" + appid + "&secret=" + secret + "";//2d6b6ec95cde8bc2908713bde4c92434  
  31.             string url = "https://api.weixin.qq.com/cgi-bin/token";  
  32.             string b = webRequestGet(url, parm);  
  33.             b = SubString1(b, "{\"access_token\":\"""\",\"expires_in\":7200}");//acc_tonke  
  34.   
  35.             
  36.             string poster = "{\"action_name\": \"QR_LIMIT_SCENE\",\"action_info\": {\"scene\": { \"scene_id\": "+scene_id+" } }";//二维码永久请求格式  
  37.   
  38.             string tockes = GetPage("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + b + "", poster + "");  
  39.             if(tockes.IndexOf("ticket")>-1)  
  40.             {  
  41.                 JsonData bejson = JsonMapper.ToObject(tockes);  
  42.                 tocket =(String)bejson["ticket"];  
  43.                 tocket = Uri.EscapeDataString(tocket);//TICKET必需UrlEncode  
  44.             }  
  45.              
  46.              
  47.             string Picurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+tocket+"";  
  48.              
  49.             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Picurl);  
  50.             req.Proxy = null;  
  51.             req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0";  
  52.             req.Headers.Add("Accept-Language""zh-cn,en-us;q=0.8,zh-hk;q=0.6,ja;q=0.4,zh;q=0.2");  
  53.             req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";  
  54.             ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };  
  55.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;  
  56.             //HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();  
  57.              
  58.             System.Drawing.Image img = System.Drawing.Image.FromStream(req.GetResponse().GetResponseStream());  
  59.             string newfilename = "ewm_" + scene_id + ".jpg";//二维码图片路径  
  60.            // string filepath = @"D:\网站项目\wsl\ewmPic\" + newfilename;  
  61.             string address = HttpContext.Current.Server.MapPath(SysConfig.ApplicationPath+"/ewmPic/"+newfilename);  
  62.             img.Save(address);  
  63.   
  64.             pic = newfilename;  
  65.         }  
  66.         return pic;  
  67.           
  68.           
  69.     }  
  70.   
  71.   
  72.     public static string SubString1(string sourceStr, string beginStr, string endStr)  
  73.     {  
  74.         int begin = sourceStr.IndexOf(beginStr);  
  75.         if (begin == -1) return "";  
  76.         int end = sourceStr.IndexOf(endStr, begin + beginStr.Length);  
  77.         if (end == -1 && end == begin + beginStr.Length) return "";  
  78.   
  79.         return sourceStr.Substring(begin + beginStr.Length, end - begin - beginStr.Length);  
  80.   
  81.     }  
  82.     /// <summary>  
  83.     /// GET 提交调用抓取  
  84.     /// </summary>  
  85.     /// <param name="url">提交地址</param>  
  86.     /// <param name="param">参数</param>  
  87.     /// <returns>string</returns>  
  88.     public static string webRequestGet(string url, string param)  
  89.     {  
  90.         byte[] bs = System.Text.Encoding.UTF8.GetBytes(param);  
  91.   
  92.         HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + "?" + param);  
  93.         req.Method = "GET";  
  94.         using (WebResponse wr = req.GetResponse())  
  95.         {  
  96.             //在这里对接收到的页面内容进行处理   
  97.   
  98.             Stream strm = wr.GetResponseStream();  
  99.   
  100.             StreamReader sr = new StreamReader(strm, System.Text.Encoding.UTF8);  
  101.   
  102.             string line;  
  103.   
  104.             System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  105.   
  106.             while ((line = sr.ReadLine()) != null)  
  107.             {  
  108.                 sb.Append(line);  
  109.             }  
  110.             sr.Close();  
  111.             strm.Close();  
  112.             return sb.ToString();  
  113.         }  
  114.     }  
  115.     public static string GetPage(string posturl, string postData)  
  116.     {  
  117.         Stream outstream = null;  
  118.         Stream instream = null;  
  119.         StreamReader sr = null;  
  120.         HttpWebResponse response = null;  
  121.         HttpWebRequest request = null;  
  122.         Encoding encoding = Encoding.UTF8;  
  123.         byte[] data = encoding.GetBytes(postData);  
  124.         // 准备请求...  
  125.         try  
  126.         {  
  127.             // 设置参数  
  128.             request = WebRequest.Create(posturl) as HttpWebRequest;  
  129.             CookieContainer cookieContainer = new CookieContainer();  
  130.             request.CookieContainer = cookieContainer;  
  131.             request.AllowAutoRedirect = true;  
  132.             request.Method = "POST";  
  133.             request.ContentType = "application/x-www-form-urlencoded";  
  134.             request.ContentLength = data.Length;  
  135.             outstream = request.GetRequestStream();  
  136.             outstream.Write(data, 0, data.Length);  
  137.             outstream.Close();  
  138.             //发送请求并获取相应回应数据  
  139.             response = request.GetResponse() as HttpWebResponse;  
  140.             //直到request.GetResponse()程序才开始向目标网页发送Post请求  
  141.             instream = response.GetResponseStream();  
  142.             sr = new StreamReader(instream, encoding);  
  143.             //返回结果网页(html)代码  
  144.             string content = sr.ReadToEnd();  
  145.             string err = string.Empty;  
  146.             return content;  
  147.         }  
  148.         catch (Exception ex)  
  149.         {  
  150.             string err = ex.Message;  
  151.             return string.Empty;  
  152.         }  
  153.     }  
  154.   
  155. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值