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;
            }
        }

  

转载于:https://www.cnblogs.com/yyyuguo/p/8426813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值