国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現微信公眾號群發消息(解決一天只能發一次的限制)實例分享

2020-01-24 03:10:11
字體:
來源:轉載
供稿:網友

總體思路:

1.首先必須要在微信公眾平臺上申請一個公眾號。

2.然后進行模擬登陸。(由于我對http傳輸原理和編程不是特別懂,在模擬登陸的地方,不是特別清楚,希望有大神指教)

3.模擬登陸后會獲得一個token(令牌)和cookie。

4.因為模擬登陸后相當于就進入了微信公眾平臺,在這個里面就可以抓取到需要的數據,如公眾好友的昵稱,fakeId。其中的fakeid非常重要,因為傳輸數據必須要知道對方的fakeid。

5.知道對方的fakeid就可以進行數據的發送了。

這里是整個項目的源碼下載:http://http://xiazai.VeVB.COm/201309/yuanma/webchat.rar

不過里面還有一些小問題,希望有人繼續修改和討論!也有人說這樣會被封號,所以請謹慎操作
講一下我項目里面的主要內容
1.WeiXinLogin.cs類是用來執行登陸功能的

復制代碼 代碼如下:

//對密碼進行MD5加密
 static string GetMd5Str32(string str)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        // Convert the input string to a byte array and compute the hash. 
        char[] temp = str.ToCharArray();
        byte[] buf = new byte[temp.Length];
        for (int i = 0; i < temp.Length; i++)
        {
            buf[i] = (byte)temp[i];
        }
        byte[] data = md5Hasher.ComputeHash(buf);
        // Create a new Stringbuilder to collect the bytes 
        // and create a string. 
        StringBuilder sBuilder = new StringBuilder();
        // Loop through each byte of the hashed data  
        // and format each one as a hexadecimal string. 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        // Return the hexadecimal string. 
        return sBuilder.ToString();
    }
//執行登陸操作
    public static bool ExecLogin(string name,string pass)
    {
        bool result = false;
        string password = GetMd5Str32(pass).ToUpper();
        string padata = "username=" + name + "&pwd=" + password + "&imgcode=&f=json";
        string url = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//請求登錄的URL
        try
        {
            CookieContainer cc = new CookieContainer();//接收緩存
            byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 轉化
            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);  //新建一個WebRequest對象用來請求或者響應url
            webRequest2.CookieContainer = cc;                                      //保存cookie 
            webRequest2.Method = "POST";                                          //請求方式是POST
            webRequest2.ContentType = "application/x-www-form-urlencoded";       //請求的內容格式為application/x-www-form-urlencoded
            webRequest2.ContentLength = byteArray.Length;
            Stream newStream = webRequest2.GetRequestStream();           //返回用于將數據寫入 Internet 資源的 Stream。
            // Send the data.
            newStream.Write(byteArray, 0, byteArray.Length);    //寫入參數
            newStream.Close();
            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
            StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
            string text2 = sr2.ReadToEnd();
            //此處用到了newtonsoft來序列化
            WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<WeiXinRetInfo>(text2);
            string token = string.Empty;
            if (retinfo.ErrMsg.Length > 0)
            {
                token = retinfo.ErrMsg.Split(new char[] { '&' })[2].Split(new char[] { '=' })[1].ToString();//取得令牌
                LoginInfo.LoginCookie = cc;
                LoginInfo.CreateDate = DateTime.Now;
                LoginInfo.Token = token;
                result = true;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.StackTrace);
        }
        return result;
    }
    public static class LoginInfo
    {
        /// <summary>
        /// 登錄后得到的令牌
        /// </summary>       
        public static string Token { get; set; }
        /// <summary>
        /// 登錄后得到的cookie
        /// </summary>
        public static CookieContainer LoginCookie { get; set; }
        /// <summary>
        /// 創建時間
        /// </summary>
        public static DateTime CreateDate { get; set; }
    }

2.在WeiXin.cs類中實現發送數據
復制代碼 代碼如下:

public static bool SendMessage(string Message, string fakeid)
    {
        bool result = false;
        CookieContainer cookie = null;
        string token = null;
        cookie = WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
        token =  WeiXinLogin.LoginInfo.Token;//取得token
        string strMsg = System.Web.HttpUtility.UrlEncode(Message);  //對傳遞過來的信息進行url編碼
        string padate = "type=1&content=" + strMsg + "&error=false&tofakeid=" + fakeid + "&token=" + token + "&ajax=1";
        string url = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
        byte[] byteArray = Encoding.UTF8.GetBytes(padate); // 轉化
        HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
        webRequest2.CookieContainer = cookie; //登錄時得到的緩存
        webRequest2.Referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token=" + token + "&fromfakeid=" + fakeid + "&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
        webRequest2.Method = "POST";
        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";
        webRequest2.ContentLength = byteArray.Length;
        Stream newStream = webRequest2.GetRequestStream();
        // Send the data.           
        newStream.Write(byteArray, 0, byteArray.Length);    //寫入參數   
        newStream.Close();
        HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
        StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
        string text2 = sr2.ReadToEnd();
        if (text2.Contains("ok"))
        {
            result = true;
        }
        return result;
    }

3.SendMessage.aspx.cs中主要實現獲取fakeid
復制代碼 代碼如下:

public static ArrayList SubscribeMP()
    {
        try
        {
            CookieContainer cookie = null;
            string token = null;
            cookie = WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
            token = WeiXinLogin.LoginInfo.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/"http://s//://s/"http://d+/""); //定義一個Regex對象實例
            mc = r.Matches(text2);
            Int32 friendSum = mc.Count;          //好友總數
            string fackID ="";
            ArrayList fackID1 = new ArrayList();
            for (int i = 0; i < friendSum; i++)
            {
                fackID = mc[i].Value.Split(new char[] { ':' })[1];
                fackID = fackID.Replace("/"", "").Trim();
                fackID1.Add(fackID);
            }
            return fackID1;
   }
        catch (Exception ex)
        {
            throw new Exception(ex.StackTrace);
        }
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌阳县| 靖边县| 万安县| 清丰县| 沽源县| 依安县| 滦南县| 中宁县| 新源县| 静乐县| 通道| 新巴尔虎右旗| 吉首市| 布拖县| 彝良县| 栾城县| 宁陕县| 蛟河市| 耒阳市| 凭祥市| 乐山市| 寿光市| 察哈| 新化县| 奈曼旗| 长泰县| 忻州市| 新巴尔虎右旗| 五峰| 牟定县| 岗巴县| 融水| 稻城县| 新野县| 东阿县| 白山市| 枣庄市| 宜州市| 涞源县| 康平县| 兴隆县|