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


    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;
                }
            }

  • 相关阅读:
    秋意浓浓回成都2月杂记
    验证表单的js代码段
    C#算法小程序(1)
    C#数据结构之单向链表
    Excel VBA编程的常用代码
    我的漫漫系分路
    2007年下半年系统分析师上午试卷及参考答案
    「2014年間休日カレンダー」のご案内
    Eclipse的几点使用技巧
    沧海一粟小组(第一次作业)
  • 原文地址:https://www.cnblogs.com/ytmom/p/3139217.html
Copyright © 2020-2023  润新知