关于微信小程序获取小程序码并接受buffer流保存为图片的方法
作者:赤原 时间:2023-07-20 09:54:01
前言
昨天因为小程序功能要获取小程序程序码,看了微信文档爬了好多坑。(留一下记录以防后面被坑)
操作
因为我获取到了微信那里的图片的图片流一直不知道怎么处理,今天总算找到相关文档,解决了。因为数据流不能直接传给前端,只好把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。觉得自己摸鱼摸得好厉害,不过终于把坑爬出来,特别开心。哈哈哈~以后要多多写开发记录。上班期间码得很随意
来源:https://www.cnblogs.com/vilva/p/10830390.html
标签:小程序,小程序码,buffer流


猜你喜欢
《JavaScript语言精粹》
2009-04-03 11:27:00
Windows 64位下python3安装nltk模块
2023-11-20 09:42:12

python自动生成model文件过程详解
2023-09-30 02:54:05
下拉框二级联动的JavaScript代码
2009-05-18 18:39:00
如何解决springboot数据库查询时出现的时区差异问题
2024-01-26 01:53:01
Python中关于使用模块的基础知识
2022-11-10 04:11:21
Go项目编写Makefile规则文件概述
2024-02-06 01:20:07
Laravel 微信小程序后端实现用户登录的示例代码
2024-06-05 15:40:52

pyshp创建shp点文件的方法
2023-06-30 03:15:29
详解python中eval函数的作用
2022-06-03 07:46:31
MySql数据库基础知识点总结
2024-01-18 13:30:20

在Python中os.fork()产生子进程的例子
2022-08-12 18:15:27
一篇文章讲解清楚MySQL索引
2024-01-28 11:59:24

浅谈JavaScript编程语言的编码规范
2010-08-18 12:08:00
python 实现自动远程登陆scp文件实例代码
2022-04-02 20:25:31
Python GUI库Tkiner使用方法代码示例
2022-03-26 04:51:29

javascript实现锁定网页、密码解锁效果(类似系统屏幕保护效果)
2023-08-18 20:01:36
如何查看python中安装库的文件位置
2021-04-17 04:09:31

canvas学习之API整理笔记(一)
2024-04-10 10:54:24

在MySQL中使用LIMIT进行分页的方法
2024-01-20 22:27:58