0
点赞
收藏
分享

微信扫一扫

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

独兜曲 2022-08-19 阅读 107

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

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

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://s2.51cto.com/images202208/15135714_62f9e03a29f9336133.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)", 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", + System.Web.HttpUtility.UrlEncode(nickName, Encoding.UTF8));
//wc.Encoding = Encoding.UTF8;

//byte[] html = wc.UploadData



//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

request.Method = "POST";
request.Referer = + 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",
wc.Encoding = Encoding.UTF8;
for (int i = 1; i <= 89; i++)
{
string html = wc.DownloadString
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

 

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

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

 

举报

相关推荐

0 条评论