生成临时wx二维码,然后下载到本地,进行调用

请求api的方法

    #region URL请求数据
        /// 
        /// HTTP POST方式请求数据
        /// 
        /// URL.
        /// POST的数据
        /// 
        public static string HttpPost(string url, string param)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;

            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();

                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }

            return responseStr;
        }

        /// 
        /// HTTP GET方式请求数据.
        /// 
        /// URL.
        /// 
        public static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }


        #endregion


生成临时二维码,然后下载到服务器

 #region 下载临时二维码,进行调用
        /// 
        /// {"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
        /// 临时二维码
        /// 
        /// 
        /// 
        Newtonsoft.Json.Linq.JObject getTicket(string accessToken, int memid)
        {

            string jsonString = "{\"expire_seconds\": \"604800\",\"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": \"" + memid + "\"}}}";
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", accessToken);
            var jsonStr = HttpPost(url, jsonString);//直接post一个json字符串
            return jsonStr.StrToJson();

        }
        /// 下载网络图片
        ///
        /// 网络地址
        /// 本地保存路径
        public void DownloadImage(string url, string path)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.ServicePoint.Expect100Continue = false;
            req.Method = "GET";
            req.KeepAlive = true;
            req.ContentType = "image/*";
            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
            System.IO.Stream stream = null;
            try
            {
                // 以字符流的方式读取HTTP响应
                stream = rsp.GetResponseStream();
                Image.FromStream(stream).Save(path);
            }
            finally
            {
                // 释放资源
                if (stream != null) stream.Close();
                if (rsp != null) rsp.Close();
            }
        }
        /// 
        /// 获取二维码保存到本地
        /// 
        /// 
        /// 
        /// 
        string getEWM(string path, int memid)
        {
            wx w = new wx();
            string at = w.access_token("wx355e13e6ae968fad", "2da7987a98ce652e3099b40bddce3595");
            var ticket = getTicket(at, memid);
            var url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket.GetValue("ticket");
            DownloadImage(url, path);
            return path;
        }

        #endregion

你可能感兴趣的:(微信,C#)