• ASP微信端开发获取用户openid


    ....

    UsersManage userbll = new UsersManage();
    UsersModels usermodel = new UsersModels();
    public void ProcessRequest(HttpContext context)
    {
    string token = "fengcheng";
    if (string.IsNullOrEmpty(token))
    {
    return;
    }

    string echoString = HttpContext.Current.Request.QueryString["echoStr"];
    string signature = HttpContext.Current.Request.QueryString["signature"];
    string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
    string nonce = HttpContext.Current.Request.QueryString["nonce"];

    if (!string.IsNullOrEmpty(echoString))
    {
    HttpContext.Current.Response.Write(echoString);
    HttpContext.Current.Response.End();
    }
    string openid = context.Request["openid"].ToString();
    string weixin = "";
    weixin = PostInput();//获取xml数据
    if (!string.IsNullOrEmpty(weixin))
    {
    ResponseMsg(weixin, context);//调用消息适配器
    }
    //和页面申请地址的一样是自己取的要和页面那个一样

    //context.Response.ContentType = "text/plain";
    //context.Response.Write(GetUserInfo("",openid)); ;
    }
    private class ExmlMsg
    {
    /// <summary>
    /// 本公众账号
    /// </summary>
    public string ToUserName { get; set; }
    /// <summary>
    /// 用户账号
    /// </summary>
    public string FromUserName { get; set; }
    /// <summary>
    /// 发送时间戳
    /// </summary>
    public string CreateTime { get; set; }
    /// <summary>
    /// 发送的文本内容
    /// </summary>
    public string Content { get; set; }
    /// <summary>
    /// 消息的类型
    /// </summary>
    public string MsgType { get; set; }
    /// <summary>
    /// 事件名称
    /// </summary>
    public string EventName { get; set; }

    }
    private ExmlMsg GetExmlMsg(XmlElement root)
    {
    ExmlMsg xmlMsg = new ExmlMsg()
    {
    FromUserName = root.SelectSingleNode("FromUserName").InnerText,
    ToUserName = root.SelectSingleNode("ToUserName").InnerText,
    CreateTime = root.SelectSingleNode("CreateTime").InnerText,
    MsgType = root.SelectSingleNode("MsgType").InnerText,
    };
    if (xmlMsg.MsgType.Trim().ToLower() == "text")
    {
    xmlMsg.Content = root.SelectSingleNode("Content").InnerText;
    }
    else if (xmlMsg.MsgType.Trim().ToLower() == "event")
    {
    xmlMsg.EventName = root.SelectSingleNode("Event").InnerText;
    }
    return xmlMsg;
    }
    #region 消息类型适配器
    private string textCase(ExmlMsg xmlMsg)
    {

    int nowtime = ConvertDateTimeInt(DateTime.Now);
    string msg = "";
    msg = getText(xmlMsg);
    string resxml = "<xml><ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
    return resxml;


    }
    private string getText(ExmlMsg xmlMsg)
    {
    string con = xmlMsg.Content.Trim();

    System.Text.StringBuilder retsb = new StringBuilder(200);
    retsb.Append("这里放你的业务逻辑");
    //retsb.Append("接收到的消息:" + xmlMsg.Content);
    retsb.Append("用户的OPEANID:" + xmlMsg.FromUserName);

    return retsb.ToString();
    }
    /// <summary>
    /// datetime转换为unixtime
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    private int ConvertDateTimeInt(System.DateTime time)
    {
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
    return (int)(time - startTime).TotalSeconds;
    }
    private void ResponseMsg(string weixin, HttpContext context)// 服务器响应微信请求
    {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(weixin);//读取xml字符串
    XmlElement root = doc.DocumentElement;
    ExmlMsg xmlMsg = GetExmlMsg(root);
    string messageType = xmlMsg.MsgType;//获取收到的消息类型。文本(text),图片(image),语音等。
    string resxml = "";

    try
    {

    switch (messageType)
    {

    case "event":
    if (!string.IsNullOrEmpty(xmlMsg.EventName) && xmlMsg.EventName.Trim() == "subscribe")
    {
    WeiXinHelper weixinhelpe = new WeiXinHelper();
    //授权页面
    string url = "http://f1t7179366.imwork.net/Reg.aspx";
    //刚关注时的时间,用于欢迎词
    int nowtime = ConvertDateTimeInt(DateTime.Now);
    string msg = "感谢您的关注,我是特工,希望多多交流" + xmlMsg.FromUserName + "<a href='"+weixinhelpe.Get_code(url)+" '>注册</a>"; //xmlMsg.FromUserName
    resxml = "<xml><ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";



    context.Response.Write(resxml);
    }
    break;
    }

    }
    catch (Exception)
    {
    context.Response.Write(resxml);
    }

    }
    #endregion
    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    #region 获取post请求数据
    /// <summary>
    /// 获取post请求数据
    /// </summary>
    /// <returns></returns>
    private string PostInput()
    {
    Stream s = System.Web.HttpContext.Current.Request.InputStream;
    byte[] b = new byte[s.Length];
    s.Read(b, 0, (int)s.Length);
    return Encoding.UTF8.GetString(b);
    }
    #endregion

    ....

  • 相关阅读:
    centos7grub2 引导win10
    必测的支付漏洞(一)——使用fiddler篡改支付金额
    使用Fiddler进行IOS APP的HTTP抓包
    Fiddler实现手机抓包——小白入门
    信息收集工具recon-ng详细使用教程
    Chrome浏览器扩展开发系列之一:初识Google Chrome扩展
    Dear Project Manager, I Hate You
    敏捷中的沟通与故事点
    项目经理与敏捷开发
    C#中使用反射获取结构体实例
  • 原文地址:https://www.cnblogs.com/yyangjing/p/6952135.html
Copyright © 2020-2023  润新知