API调用微信getWXACodeUnlimit()获取小程序码
程序员文章站
2022-10-17 10:13:21
微信文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/qr-code/getWXACodeUnlimit.html?client=tim&ADUIN=2312459330&ADSESSION=1540344656&A ......
代码:
public jsonr miniappcode(string scene, string page, int width, bool auto_color, object line_color, bool is_hyaline) { var accesstoken = wx.getaccesstoken("appid*************", "appsecret******************************");//获取接口accesstoken var url = string.format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}", accesstoken); var postdata = new { scene, page, width, auto_color, line_color, is_hyaline, }.tojson(); system.net.httpwebrequest request; request = (system.net.httpwebrequest)webrequest.create(url); request.method = "post"; request.contenttype = "application/json;charset=utf-8"; byte[] payload; payload = system.text.encoding.utf8.getbytes(postdata); request.contentlength = payload.length; stream writer = request.getrequeststream(); writer.write(payload, 0, payload.length); writer.close(); system.net.httpwebresponse response; response = (system.net.httpwebresponse)request.getresponse(); system.io.stream stream; stream = response.getresponsestream(); list<byte> bytes = new list<byte>(); int temp = stream.readbyte(); while (temp != -1) { bytes.add((byte)temp); temp = stream.readbyte(); } byte[] result = bytes.toarray(); return comenum.code.a_操作成功.jsonr(result); }
微信调用成功返回的是图片二进制内容,这里做了处理,返回给前台的是byte数组
前台用img标签显示:<img src='data:image/jpeg;base64,+result+'/>