前言
记录编写牛奶咖啡软件的相关代码,方便后续自己修改更新改进
相关代码
private async void 更新标识()
{
string url = "http://rc.leeuu.com/data/js/rcdef01.js";//浪漫庄园更新地址
using HttpClient client = new HttpClient();
string pagecontent = await client.GetStringAsync(url);
string pattern = @"news,(.*?),";
Match match = Regex.Match(pagecontent, pattern);
if (match.Success)
{
string extractedContent = match.Groups[1].Value;
Console.WriteLine(extractedContent);
string url_更新内容 = "http://rc.leeuu.com/data/news/" + extractedContent + ".htm";
using HttpClient client1 = new HttpClient();
string pagecontent1 = await client1.GetStringAsync(url_更新内容);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(pagecontent1);
HtmlNode 内容 = doc.DocumentNode.SelectSingleNode("//div[@id='right']");//获取更新内容的相关信息
if (内容 != null)
{
Console.WriteLine(内容.InnerHtml);
string 正文 = 内容.InnerHtml;
//去掉p标签,去掉</strong>标签
正文 = 正文.Replace("<p>", "").Replace("</p>", "").Replace("<strong>", "").Replace("</strong>", "").Replace("<h3>", "").Replace("</h3>", "").Replace("<p style=\"text-align: right;\">", "").Replace(" ", "").Replace("</span>", "").Replace("<span style=\"text-indent: 2em;\">", "").Replace("<br>", "").Replace("<div id=\"qb-sougou-search\" style=\"display: none; opacity: 0;\">\r\n搜索\r\n<p class=\"last-btn\">复制\r\n<iframe src=\"\"></iframe></div>", "").Replace("<span style=\"color: rgb(255, 0, 255);\">", "");//去除无关内容
textBox_首页.Text = 正文;
}
else
{
Console.WriteLine("没找到相关内容");
}
}
else
{
Console.WriteLine("未找到匹配内容");
}
}
{/collapse-item}
{collapse-item label="获取服务器更新说明" close}
说明:读取服务器内的文本文件获取其中内容,并将获取到的内容显示在指定位置,代码如下:
private async Task 历史更新()
{
string 历史更新_url = "........";//将""中的内容替换为你自己的文本文档地址
try
{
WebClient client = new WebClient();
string content = client.DownloadString(历史更新_url);
// 将换行符替换为TextBox能识别的换行格式
content = content.Replace("\n", "\r\n");
textBox_历史更新.Text = content;
}
catch (Exception ex)
{
MessageBox.Show("读取文件出错: " + ex.Message);
}
}
{/collapse-item}
if (comboBox_功能选择.SelectedIndex == 0)
{
string 搜物_url = ".....";//替换为你的文档位置
try
{
WebClient client = new WebClient();
string content = client.DownloadString(搜物_url);
string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
comboBox_查询.Items.Clear();
foreach (string line in lines)
{
comboBox_查询.Items.Add(line);
}
}
catch (Exception ex)
{
MessageBox.Show("读取文件出错: " + ex.Message);
}
}
其它的内容类似
{/collapse-item}
{collapse-item label="钉钉发送信息函数" close}
说明:钉钉发送信息函数,代码如下:
public static class DingDingNotifier
{
private const string WebhookUrl = "......";//替换为钉钉群内机器人的链接
public static async Task SendMessage(string message)
{
var client = new HttpClient();
// 使用 markdown 格式并包含 @all
var jsonContent = new
{
msgtype = "markdown",
markdown = new
{
title = "公告",
text = $"#### 公告\n{message}\n\n@all" // 注意这里的 @all 标签
},
at = new
{
atMobiles = new string[] , // 保持为空数组,因为我们使用 @all
isAtAll = true // 设置为 true 以 @所有人
}
};
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(jsonContent, jsonOptions), Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(WebhookUrl, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"发送消息到钉钉失败。状态码: {response.StatusCode}");
}
else
{
Console.WriteLine("消息发送成功。");
}
}
catch (Exception ex)
{
Console.WriteLine($"发送消息到钉钉时出错: {ex.Message}");
}
}
}
{/collapse-item}