生成小程序个人二维码

1.要有appid和密钥

2.获取token

        public string get_token()
        {
            string wx_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf050f84e644a8077&secret=65554946e2a6a47b8aa1c56a4ae1edc7";
            wx_url = string.Format(wx_url);
            string wx_result = Utils.HttpGet(wx_url);
            JObject jo = JObject.Parse(wx_result);
            string access_token = "";
            if (jo["access_token"] != null)
            {
                access_token = Utils.ObjectToStr(jo["access_token"]);
            }
            return access_token;
        }

3.生成二维码,保存图片

        public string QR_code(string md5, string sendtime, string page, string scene = "")
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            jsSerializer.MaxJsonLength = Int32.MaxValue;
            string appid = "1314520";
            string key = "hV72WsDa37LPZxQSjL";
            DateTime time = Convert.ToDateTime(sendtime);
            TimeSpan s1 = DateTime.Now - time;
            if (s1.Minutes > 10)
            {
                return "{\"status\": 0, \"msg\": \"请求时间失效了!\"}";
            }
            string str = appid + key + sendtime;
            string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();
            if (sign != md5)
            {
                return "{\"status\": 0, \"msg\": \"加密字符串不正确!\"}";
            }
            string width = "460";
            string access_token = get_token();
            string wx_url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + access_token + "";
            string wx_result = HttpPost(wx_url, "{\"path\":\"" + page + "\",\"width\": \"430\"}");//pages/index?query=1\
            Image image1 = Base64ToImage(wx_result);
            DateTime dt = DateTime.Now;
            string path = "/upload/201806/22/" + dt.ToString("yyyyMMddHHmmss") + ".jpg";
            image1.Save(Server.MapPath(path));
            return "{ \"status\":1,\"imgurl\":\"" + path + "\",\"data\":\"data:image/jpg;base64," + wx_result + "\"}";

        }
        //threeebase64编码的字符串转为图片  
        public System.Drawing.Image Base64ToImage(string base64String)
        {
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            return image;
        }
        [WebMethod]
        public string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.Default);
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            try
            {
                Image image = Image.FromStream(myResponseStream);
                myResponseStream.Close();
                string retString = ToBase64(image);
                return retString;
            }
            catch
            {

                myResponseStream.Close();
                return "";
            }
        }
        [WebMethod]
        public string ToBase64(Image img)
        {

            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            String strbaser64 = Convert.ToBase64String(arr);
            return strbaser64;
        }

 

你可能感兴趣的:(小程序,接口,.net)