webapi通過微信api獲取二維碼buffer流轉換成圖片保存到本地


前言:微信小程序需要生成二維碼,后台通過微信api獲取buffer流轉換成圖片保存到本地。以下代碼直接返回圖片地址

  public static string Api_Post(string postUrl, string postData, WebHeaderCollection header = null, bool isPic = false)
        {
            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/json";
                if (header != null) request.Headers = header;
                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();

                if (isPic)
                {
                    byte[] tt = StreamToBytes(instream);//將數據流轉為byte[]
                    string guid = Guid.NewGuid().ToString();
                    System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath("~/Content/img/"+ guid + ".jpg"), tt);
                   
                    return "https://*********/Content/img/" + guid + ".jpg";
                }
                else
                {
                    sr = new StreamReader(instream, encoding);
                    //返回結果網頁(html)代碼
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content;
                }

            }
            catch (Exception ex)
            {
                if (isPic)
                {
                    sr = new StreamReader(instream, encoding);
                    //返回結果網頁(html)代碼
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content;
                }
                else
                {
                    string err = ex.Message;
                    return string.Empty;
                }
            }
        }
  ///將數據流轉為byte[]
        public static byte[] StreamToBytes(Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();
        }
 
         

 

 
 

調用上面Api_Post方法

 string strUrlPara = "{\"page\":\"" + page + "\",\"scene\":" + scene + "}";
 string url= "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=**************";
 string resu = Api_Post(url, strUrlPara, null, true);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM