国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

關于微信小程序獲取小程序碼并接受buffer流保存為圖片的方法

2020-03-21 15:54:24
字體:
來源:轉載
供稿:網友

前言

昨天因為小程序功能要獲取小程序程序碼,看了微信文檔爬了好多坑。(留一下記錄以防后面被坑)

操作

因為我獲取到了微信那里的圖片的圖片流一直不知道怎么處理,今天總算找到相關文檔,解決了。因為數據流不能直接傳給前端,只好把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/x-www-form-urlencoded";        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[]          System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath("~/WxCode.jpg"), tt);          WxQRCodeModel model = new WxQRCodeModel();          model.data = "192.168.1.216:80/WxCode.jpg";          model.errcode = 0;          string content = Config.js.Serialize(model);          string err = string.Empty;          return content;        }        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;        }      }    }

因為是instream接受到微信接口那里發送過來的數據流,就在instream那里處理,把數據流轉換為byte[]數組,然后依靠File的WriteAllBytes方法把轉換OK的byte[]數組轉換為圖片存放在服務器上,然后把圖片路徑交給model。

    ///將數據流轉為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();    }

結尾

最近才接觸到微信小程序開發,emmmm。覺得自己摸魚摸得好厲害,不過終于把坑爬出來,特別開心。哈哈哈~以后要多多寫開發記錄。上班期間碼得很隨意

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望奎县| 云林县| 巴东县| 营山县| 新巴尔虎左旗| 乐山市| 乐平市| 东乌珠穆沁旗| 南江县| 朝阳区| 正宁县| 安仁县| 霍州市| 酒泉市| 太保市| 金溪县| 耿马| 视频| 博客| 农安县| 阿合奇县| 诸暨市| 久治县| 东兰县| 汝城县| 佛冈县| 永春县| 祁阳县| 巴林右旗| 呈贡县| 大石桥市| 旺苍县| 中江县| 富裕县| 祁连县| 麻城市| 阜新市| 台北县| 红安县| 普兰店市| 宜川县|