.net 实现微信公众平台的用户信息获取

 2023-09-16 阅读 15 评论 0

摘要:public string getSubUserFkId() { log.Info("开始去微信服务器获取关注用户信息!");//大家可以把这一句注掉 try { CookieContainer cookie = null; string token = null; //此处的作用是判断Cookie是否过期如果过期就重新获取,获取cookie的方法本人

public string getSubUserFkId()
        {
            log.Info("开始去微信服务器获取关注用户信息!");//大家可以把这一句注掉
            try
            {
                CookieContainer cookie = null;
                string token = null;

                //此处的作用是判断Cookie是否过期如果过期就重新获取,获取cookie的方法本人在.net 实现微信公众平台的主动推送信息中有源码。大家可以去看一下。这里就不再粘源代码了。

                if (null == LoginPassWord.cookie || LoginPassWord.CreateDate.AddMinutes(Convert.ToInt32(WeiXinBase.readxml.ReadDataBaseConStr("LoingMinutes"))) < DateTime.Now)
                {
                    new LoginBase().ExecLogin();
                }
                cookie = LoginPassWord.cookie;//取得cookie
                token = LoginPassWord.token;//取得token

/*获取用户信息的url,这里有几个参数给大家讲一下,1.token此参数为上面的token 2.pagesize此参数为每一页显示的记录条数

3.pageid为当前的页数,4.groupid为微信公众平台的用户分组的组id,当然这也是我的猜想不一定正确*/
                string Url = "https://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&token="+token+"&lang=zh_CN&pagesize=10&pageidx=0&type=0&groupid=0";
                HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(Url);
                webRequest2.CookieContainer = cookie;
                webRequest2.ContentType = "text/html; charset=UTF-8";
                webRequest2.Method = "GET";
                webRequest2.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
                webRequest2.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
                StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
                string text2 = sr2.ReadToEnd();
                MatchCollection mc;

如何编辑微信公众号框架?//由于此方法获取过来的信息是一个html网页所以此处使用了正则表达式,注意:(此正则表达式只是获取了fakeid的信息如果想获得一些其他的信息修改此处的正则表达式就可以了。)
                Regex r = new Regex("\"fakeId\"\\s\\:\\s\"\\d+\""); //定义一个Regex对象实例
                mc = r.Matches(text2);
                //string fackId = mc[0].Value.Split(new char[] { ':'})[1];
                fackId = fackId.Replace("\"", "").Trim();
                return fackId;
            }
            catch(Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
        }

转载于:https://www.cnblogs.com/ytmom/p/3139217.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/70801.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息