码迷,mamicode.com
首页 > 其他好文 > 详细

想想就好:这些年,博客园没有变,变的只是我们

时间:2018-05-30 21:30:19      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:form   threading   pre   内心   method   useragent   private   put   toe   

今天想着对关注自己的粉丝群发条消息,想到博客园没提供这功能,那就自己来吧:

于是自己写了段代码,采集昵称,并模拟发送:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Cnblogs_SendMsg
{
    class Program
    {

        static void Main(string[] args)
        {
            // GatherNickNames();
            SendMsg();
        }

        #region 向粉丝发送消息
        static void SendMsg()
        {
            string[] nicks = File.ReadAllLines(path, Encoding.Default);
            Random rnd = new Random();
            for (int i = 0; i < nicks.Length; i++)
            {
                Send(nicks[i], nicks[i] + ",感谢关注我的博客!", @"感谢您关注我的博客,同时也欢迎关注我的微信公众号
![](https://images2018.cnblogs.com/blog/17408/201805/17408-20180523041027505-1002652922.jpg)", i);
                Thread.Sleep(60*1000 + rnd.Next(1500, 2000));
            }

        }
        private static void Send(string nickName, string title, string text, int index)
        {
            try
            {
                string json = "{content:\"" + text + "\",name:\"" + nickName + "\",title:\"" + title + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(json);
                //WebClient wc = new WebClient();
                //wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
                //wc.Headers.Add("cookie", cookie);
                //wc.Headers.Add("content-type", "application/json; charset=UTF-8");
                //wc.Headers.Add("referer", "https://msg.cnblogs.com/send?recipient=" + System.Web.HttpUtility.UrlEncode(nickName, Encoding.UTF8));
                //wc.Encoding = Encoding.UTF8;
                
                //byte[] html = wc.UploadData("https://msg.cnblogs.com/api/message", data);
               


                //string cookieStr = "51fd9f14fa7561b5";
                //string postData = string.Format("userid={0}&password={1}", "guest", "123456");
                //byte[] data = Encoding.UTF8.GetBytes(postData);

                // Prepare web request...  
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msg.cnblogs.com/api/message");
                
                request.Method = "POST";
                request.Referer = "https://msg.cnblogs.com/send?recipient=" + System.Web.HttpUtility.UrlEncode(nickName, Encoding.UTF8);
                request.ContentType = "application/json; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537."+DateTime.Now.Second;
                request.Headers.Add("Cookie", cookie);
                request.ContentLength = data.Length;
                Stream newStream = request.GetRequestStream();

                // Send the data.  
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                // Get response  
                HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                string err = myResponse.Headers["X-Error"];
                if (!string.IsNullOrEmpty(err))
                {
                    Console.WriteLine(index + " : err " + err);
                    Thread.Sleep(60 * 1000 * 5);
                    Send(nickName, title, text, index);
                }
                else
                {
                    Console.WriteLine(index + " : ok " + nickName);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(index + " : err " + nickName + " : " + err.Message);
            }
        }
        #endregion
        #region 采集粉丝的昵称


        static void GatherNickNames()
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
                wc.Headers.Add("cookie", cookie);
                wc.Headers.Add("referer", "https://home.cnblogs.com/followers/");
                wc.Encoding = Encoding.UTF8;
                for (int i = 1; i <= 89; i++)
                {
                    string html = wc.DownloadString("https://home.cnblogs.com/followers/?page=" + i);
                    GetNickName(html);
                    Console.WriteLine(i);
                    Thread.Sleep(500);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }
        private static void GetNickName(string html)
        {
            int startIndex = html.IndexOf("class=\"avatar_list\">");
            if (startIndex == -1) { return; }
            int endIndex = html.IndexOf("</ul>", startIndex);
            if (startIndex > endIndex) { return; }

            string text = html.Substring(startIndex, endIndex - startIndex);

            MatchCollection mc = Regex.Matches(text, "title=\"(.+)\"");
            if (mc != null && mc.Count > 0)
            {
                string[] names = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    names[i] = mc[i].Groups[1].Value;
                }
                File.AppendAllLines(path, names, Encoding.Default);

            }

        }
        #endregion

        static string path = AppDomain.CurrentDomain.BaseDirectory + "nicks.txt";
        static string cookie = @"";
    }
}

然而发现博客园对发送的次数有严格的限制,多数情况下你会得到一个:SendTooManyMessages 

 

就当我内心开始对博客园又产生一丝抱怨的情感时,就在那一瞬间,我好像明白了什么。

博客园还是当年那个博客园,只是,我们变了,变的不那么的纯理想主义了。

 

想想就好:这些年,博客园没有变,变的只是我们

标签:form   threading   pre   内心   method   useragent   private   put   toe   

原文地址:https://www.cnblogs.com/cyq1162/p/9112838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!