• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信小程序获取二维码API

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<%@ WebHandler Language="C#" Class="ce" %>

using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;
using LitJson;
using System.Web.Script.Serialization;

public class ce : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //微信access_tocke
        string ulr = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + this.access_token();

        HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(ulr);  //创建url
        request.Method = "POST";
        request.ContentType = "application/json;charset=UTF-8";
        JsonData data = new JsonData();
        data["path"] = "\'pages/index?query=1";
        data["width"] = "430";

        string json = data.ToJson();
        byte[] load = Encoding.UTF8.GetBytes(json);
        request.ContentLength = load.Length;
        Stream writer = request.GetRequestStream();
        writer.Write(load,0,load.Length);


        HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        Stream s = response.GetResponseStream();
        byte[] mg = StreamToBytes(s);
        context.Response.BinaryWrite(mg);

    }

    public string access_token() {
        string urls = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=1111&secret=1111";
        HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(urls);  //请求api
        HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        string respText = "";
        using (Stream resStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(resStream, Encoding.Default);
            respText = reader.ReadToEnd();
            resStream.Close();
        }
        JavaScriptSerializer Jss = new JavaScriptSerializer();
        Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
        //通过键access_token获取值
        return respDic["access_token"].ToString();



    }

    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();

    }



    public bool IsReusable {
        get {
            return false;
        }
    }

}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序获取Access_token和页面URL生成小程序码或二维码发布时间:2022-07-18
下一篇:
小程序二维码解码发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap