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

首頁 > 熱點 > 微信 > 正文

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

2024-07-22 01:18:14
字體:
來源:轉載
供稿:網友

前言

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

操作

因為我獲取到了微信那里的圖片的圖片流一直不知道怎么處理,今天總算找到相關文檔,解決了。因為數據流不能直接傳給前端,只好把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。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 游戏| 河北区| 永德县| 介休市| 汕尾市| 沁源县| 来宾市| 平南县| 普兰店市| 高台县| 东方市| 临桂县| 南郑县| 乃东县| 巨鹿县| 九台市| 门源| 兴文县| 天等县| 新乡县| 鄂托克旗| 互助| 大余县| 越西县| 沧州市| 平利县| 嵊州市| 镇康县| 鄂托克前旗| 金沙县| 二连浩特市| 定边县| 若羌县| 襄垣县| 临江市| 鲁甸县| 丹阳市| 天全县| 武定县| 平定县| 庐江县|