关于微信小程序获取小程序码并接受buffer流保存为图片的方法

(编辑:jimmy 日期: 2025/4/8 浏览:2)

前言

昨天因为小程序功能要获取小程序程序码,看了微信文档爬了好多坑。(留一下记录以防后面被坑)

操作

因为我获取到了微信那里的图片的图片流一直不知道怎么处理,今天总算找到相关文档,解决了。因为数据流不能直接传给前端,只好把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。觉得自己摸鱼摸得好厉害,不过终于把坑爬出来,特别开心。哈哈哈~以后要多多写开发记录。上班期间码得很随意

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。

一句话新闻

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?