C# · 12月 19, 2021

写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我

写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我

1、HttpUtil工具类,用于模拟用户登录以及爬取网页:

using System; System.Collections.Generic; System.IO; System.Linq; System.Net; System.Text; System.Threading;namespace Utils{ /// <summary> /// Http上传下载文件 </summary> public class HttpUtil { #region cookie设置 private static CookieContainer m_Cookie = new CookieContainer(); static void SetHttpCookie(CookieContainer cookie) { m_Cookie = cookie; } #endregion #region HttpDownloadFile 下载文件 static MemoryStream HttpDownloadFile(string url) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = “GET”; request.CookieContainer = m_Cookie; 发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() HttpWebResponse; 直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); 创建写入流 MemoryStream stream = MemoryStream(); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr,0,(int)bArr.Length); while (size > 0) { stream.Write(bArr,,size); size = responseStream.Read(bArr,1)”>)bArr.Length); } stream.Seek(return stream; } #region HttpUploadFile 上传文件 <summary> Http上传文件 </summary> string HttpUploadFile(string url,byte[] bArr,1)”> fileName) { HttpWebRequest; CookieContainer cookieContainer = CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = POST; string boundary = DateTime.Now.Ticks.ToString(X”); 随机分隔线 request.ContentType = text/plain;charset=utf-8 m_Cookie; Stream postStream = request.GetRequestStream(); postStream.Write(bArr,bArr.Length); postStream.Close(); 直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = StreamReader(instream,Encoding.UTF8); 返回结果网页(html)代码 string content = sr.ReadToEnd(); content; } #region HttpPost HttpPost string HttpPost( data) { byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data); HttpWebRequest; request.CookieContainer = m_Cookie; request.Method = ; request.ContentType = application/x-www-form-urlencoded; request.ContentLength = bArr.Length; request.UserAgent = Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0); Stream postStream = response.GetResponseStream(); 返回结果网页(html)代码 MemoryStream memoryStream = MemoryStream(); bArr = ) { memoryStream.Write(bArr,1)”>)bArr.Length); Thread.Sleep(1); } Encoding.UTF8.GetString(memoryStream.ToArray()); ; request.UserAgent = ; MemoryStream(); #region HttpGet HttpGet string HttpGet(#endregion }}View Code

2、Windows服务代码:

System.ComponentModel; System.Configuration; System.Data; System.Diagnostics; System.Runtime.InteropServices; System.ServiceProcess; System.Text.RegularExpressions; System.Threading; System.Threading.Tasks; System.Timers; System.Windows.Forms; Utils; BugMonitor{ partial BugMonitorService : ServiceBase { [DllImport(kernel32.dll”,SetLastError = )] extern WTSGetActiveConsoleSessionId(); [DllImport(wtsapi32.dllbool WTSSendMessage( IntPtr hServer, SessionId,String pTitle,1)”> TitleLength,String pMessage,1)”> MessageLength,1)”> Style,1)”> Timeout,1)”>out pResponse,1)”> bWait); static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; private System.Timers.Timer timer; static List<int> idList = new List<int>(); string loginUrl = ConfigurationManager.AppSettings[loginUrl]; string listUrl = ConfigurationManager.AppSettings[bugListUrlstring userLogin = ConfigurationManager.AppSettings[userNamestring userPassword = ConfigurationManager.AppSettings[userPasswordprivate Regex regTr = new Regex(@”<tr class=””listTableLine(?:(?!</tr>)[\s\S])*</tr>private Regex regTd = <td align=””left””>((?:(?!</td>)[\s\S])*)</td>int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings[pageSize]); double interval = Convert.ToDouble(ConfigurationManager.AppSettings[intervalstring projectId = ConfigurationManager.AppSettings[projectId]; public BugMonitorService() { InitializeComponent(); } void ShowMessage@R_327_2419@(string message,1)”> title) { int resp = ; WTSSendMessage( WTS_CURRENT_SERVER_HANDLE,WTSGetActiveConsoleSessionId(),title,title.Length,message,message.Length,1)”>out resp,1)”>false); } protected override void OnStart([] args) { LogUtil.path = Application.StartupPath + \\log; timer = new System.Timers.Timer(interval * 60 * 1000); timer.Elapsed += System.Timers.ElapsedEventHandler(Action); timer.Start(); LogUtil.Log(服务启动成功 OnStop() { if (timer != null) { timer.Stop(); timer.Close(); timer.Dispose(); timer = ; } LogUtil.Log(服务停止成功); Thread.Sleep(100); 等待一会,待日志写入文件 } Start() { OnStart(void Action(object sender,ElapsedEventArgs e) { try { Task.Factory.StartNew(() => { { int bugCount = ; string loginResult = HttpUtil.HttpPost(loginUrl,1)”>string.Format(uer={0}&userPassword={1}&submit=%E7%99%BB%E5%BD%95&userLogin={0}&uer=string result = HttpUtil.HttpPost(listUrl,1)”>projectId={0}&perListDPF={1}&sortFieldDPF=bugCode&sortSequenceDPF=1&bugStatus=1ref bugCount); result = HttpUtil.HttpPost(listUrl,1)”>projectId={0}&perListDPF={1}&sortFieldDPF=bugCode&sortSequenceDPF=1&bugStatus=3 bugCount); if (bugCount > ) { ShowMessage@R_327_2419@(您有 {0} 个新BUG提醒); } else { LogUtil.Log(没有新BUG); } } catch (Exception ex) { LogUtil.LogError(ex.Message + \r\n” + ex.StackTrace); } }); } (Exception ex) { LogUtil.LogError(ex.Message + ex.StackTrace); } } void ProcessBug(string bugListPageHtml,1)”>ref bugCount) { MatchCollection mcTr = regTr.Matches(bugListPageHtml); foreach (Match mTr in mcTr) { MatchCollection mcTd = regTd.Matches(mTr.Value); if (mcTd.Count > ) { int id = Convert.ToInt32(mcTd[0].Groups[].Value.Trim()); string strStatus = mcTd[].Value.ToLower(); if (!idList.Exists(a => a == id)) { if (strStatus.IndexOf(已激活”) > 0 || strStatus.IndexOf(重新打开) { idList.Add(id); bugCount++; LogUtil.Log(发现新的BUG,BUG编号:{0}View Code