• c# 微信开发 《生成带参数的关注二维码》


    /// <summary>
            /// 生成二维码
            /// </summary>
            /// <returns></returns>
            /// 申请生成二维码的用户 CreatQrCodeUser
            public string CreateQrCodeImage(string CreatQrCodeUser)
            {
                string WeixinQrCodePath = ConfigurationManager.AppSettings["WeixinQrCodePath"].TryToString(); //配置的文件地址
                string rootpath = System.AppDomain.CurrentDomain.BaseDirectory;//程序运行地址
                string path = rootpath + WeixinQrCodePath; //文件夹绝对路径
                string filename = Guid.NewGuid().TryString() + ".jpg";
                string absolutelyPath = path + "/" +filename; //生成的二维码绝对路径
                string returnPath = "";//文件返回的路径
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                int Sceneid = WeixinDA.GetQrcodeImgMaxSceneid(CreatQrCodeUser);
                //保存二维码生成的图片
                WeixinQrCode.SaveQrCodeImage(Sceneid.TryString(), absolutelyPath);
    
                //最终生成二维码
                string ResultCodeImg = path + "/" + filename; //最终合成图片的绝对路径
                returnPath = WeixinQrCodePath + "/" + filename;//最终合成图片的相对路径
                WeixinDA.SaveUserQrcodeImg(CreatQrCodeUser, Sceneid, returnPath);
                return returnPath;
            }

    /// <summary>
    /// 获取二维码图片Url,需要Format 0.ticket
    /// </summary>
    public const string WeiXin_QrCode_GetUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}";

     /// <summary>
            /// 根据Ticket获取二维码图片保存在本地
            /// </summary>
            /// <param name="scene_id">二维码场景id</param>
            /// <param name="imgPath">图片存储路径</param>
            public static void SaveQrCodeImage(string scene_id, string imgPath)
            {
                try
                {
                    string Ticket = WeiXinCommom.CreateTicket(scene_id);
    
                    if (Ticket == null)
                        throw new ArgumentNullException("Ticket");
    
                    //ticket需 urlEncode
                    string stUrl = string.Format(WeiXinCommom.WeiXin_QrCode_GetUrl, HttpUtility.UrlEncode(Ticket));
                    WeiXinCommom.SaveUrlImage(stUrl, imgPath);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteFileLog("qrcodeError", ex.Message);
                }
            }
    
    public const string WeiXin_Ticket_CreateUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";
    
    public const string QrCodeTicket_Create_TempJsonStrin = "{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": {0}}}}";
    
     /// <summary>
            /// 获取Ticket
            /// </summary>
            /// <returns></returns>
            public static string CreateTicket(string scene_id)
            {
                string access_token = WeiXinCommom.GetAccsss_token(); ;
    
                if (string.IsNullOrEmpty(access_token))
                    throw new ArgumentNullException("Access_Token");
    
                string url = string.Format(WeiXin_Ticket_CreateUrl, access_token);
                string postData = QrCodeTicket_Create_TempJsonStrin.Replace("{0}", scene_id);
                string result = PostData(url, postData);
                WeixinQrCode.Ticket ticket = JsonConvert.DeserializeObject<WeixinQrCode.Ticket>(result);
    
                if (ticket == null || string.IsNullOrEmpty(ticket.ticket))
                {
                    FileLog.AddLog("ticket", "获取二维码ticket失败");
                    return "";
                }
                return ticket.ticket;
            }
    
     /// <summary>
            /// Post数据
            /// </summary>
            /// <param name="posturl"></param>
            /// <param name="postData"></param>
            /// <returns></returns>
            public static string PostData(string posturl, string postData)
            {
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = Encoding.UTF8;
                byte[] data = encoding.GetBytes(postData);
                // 准备请求...
                try
                {
                    // 设置参数
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content;
                }
                catch (Exception ex)
                {
                    LogHelper.WriteFileLog("PostDataerror", string.Format("{0}", ex.Message));
                    return string.Empty;
                }
            }
    

      

  • 相关阅读:
    HAL驱动的串口编程陷阱
    ST推出新软件STM32Cube ,让STM32微控制器应用设计变得更容易、更快、更好用
    零宽断言 -- Lookahead/Lookahead Positive/Negative
    Eclipse 正则表达式 查找与替换
    MSDN WinUSB Example
    培训SQLServer 嵌套事务PPT分享
    SQL Server数据库备份的镜像
    禁用nested loop join里的spool
    日常办公工具利器 notepad++
    Change the Target Recovery Time of a Database (SQL Server) 间接-checkpoints flushcache flushcache-message
  • 原文地址:https://www.cnblogs.com/yyyuguo/p/8426813.html
Copyright © 2020-2023  润新知