C implements the WeChat public number to send messages of to solve the problem of limited instance sharing which can only be sent once a day

  • 2020-05-19 05:31:21
  • OfStack

General idea:

1. You must first apply for a public account on the WeChat public platform.

2. Then simulate landing. (since I don't know much about the transmission principle and programming of http, I don't know much about it in the simulated landing place. I hope you can give me some guidance.)

3. After simulated login, one token (token) and cookie will be obtained.

4. Because the simulated login is equivalent to entering the WeChat public platform, in which the required data can be captured, such as the nickname of public friends, fakeId. The fakeid is very important, because the transmission data must know the fakeid of the other party.

5. If you know the fakeid of the other party, you can send the data.

Here is the source of the project download: http: / / http: / / xiazai ofstack. com / 201309 / yuanma webchat. rar

But there are still 1 small problems inside, hope someone continue to modify and discuss! Some people say that this will be blocked, so please be careful
Let's talk about the main content of my project
1.WeiXinLogin.cs class is used to perform the login function


// Perform on the password MD5 encryption 
 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();
    }
// Perform the login operation 
    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 ";// Requested logon URL
        try
        {
            CookieContainer cc = new CookieContainer();// Receive buffer 
            byte[] byteArray = Encoding.UTF8.GetBytes(padata); //  conversion 
            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);  // new 1 a WebRequest Object is used to request or respond url
            webRequest2.CookieContainer = cc;                                      // save cookie  
            webRequest2.Method = "POST";                                          // The request method is POST
            webRequest2.ContentType = "application/x-www-form-urlencoded";       // The requested content format is application/x-www-form-urlencoded
            webRequest2.ContentLength = byteArray.Length;
            Stream newStream = webRequest2.GetRequestStream();           // Return is used to write data  Internet  resources  Stream . 
            // Send the data.
            newStream.Write(byteArray, 0, byteArray.Length);    // In parameter 
            newStream.Close();
            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
            StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
            string text2 = sr2.ReadToEnd();
            // It's used here newtonsoft To serialize 
            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();// Get the token 
                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>
        ///  The token you get after logging in 
        /// </summary>        
        public static string Token { get; set; }
        /// <summary>
        ///  Log in and get it cookie
        /// </summary>
        public static CookieContainer LoginCookie { get; set; }
        /// <summary>
        ///  Creation time 
        /// </summary>
        public static DateTime CreateDate { get; set; }
    }

2. Send data in the WeiXin.cs class

public static bool SendMessage(string Message, string fakeid)
    {
        bool result = false;
        CookieContainer cookie = null;
        string token = null;
        cookie = WeiXinLogin.LoginInfo.LoginCookie;// achieve cookie
        token =  WeiXinLogin.LoginInfo.Token;// achieve token
        string strMsg = System.Web.HttpUtility.UrlEncode(Message);  // You're going to do it with the information that's coming through url coding 
        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); //  conversion 
        HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
        webRequest2.CookieContainer = cookie; // The cache you get when you log in 
        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);    // In parameter     
        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;
    }

The main implementation in SendMessage.aspx.cs obtains fakeid

public static ArrayList SubscribeMP()
    {
        try
        {
            CookieContainer cookie = null;
            string token = null;
            cookie = WeiXinLogin.LoginInfo.LoginCookie;// achieve cookie
            token = WeiXinLogin.LoginInfo.Token;// achieve token
            /* To obtain user information url Here are a few parameters for you 1 Next, 1.token This parameter is above token 2.pagesize This parameter is each 1 Page number of record bars displayed 
            3.pageid Is the current page number, 4.groupid A group that groups users for the WeChat public platform id Of course that's my guess no 1 On the right */
            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;
            // Because the information that this method retrieves is 1 a html Web pages use regular expressions here, note :(this regular expression is only retrieved fakeid Information if you want to get it 1 Some additional information can be used to modify the regular expression here. 
             Regex r = new Regex("\"fakeId\"\\s\\:\\s\"\\d+\""); // define 1 a Regex Object instance 
            mc = r.Matches(text2);
            Int32 friendSum = mc.Count;          // The total number of friends 
            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);
        }
    }


Related articles: