1. 程式人生 > >API呼叫微信getWXACodeUnlimit()獲取小程式碼

API呼叫微信getWXACodeUnlimit()獲取小程式碼

程式碼:

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+'/>