相思资源网 Design By www.200059.com

前言

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

操作

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

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

标签:
小程序获取小程序码,小程序,buffer流保存

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

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

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

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。