写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我...
生活随笔
收集整理的這篇文章主要介紹了
写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫了一個Windows服務,通過C#模擬網站用戶登錄并爬取BUG列表查詢有沒有新的BUG,并提醒我
1、HttpUtil工具類,用于模擬用戶登錄以及爬取網頁:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading;namespace Utils {/// <summary>/// Http上傳下載文件/// </summary>public class HttpUtil{#region cookie設置private static CookieContainer m_Cookie = new CookieContainer();public static void SetHttpCookie(CookieContainer cookie){m_Cookie = cookie;}#endregion#region HttpDownloadFile 下載文件public static MemoryStream HttpDownloadFile(string url){// 設置參數HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;request.Method = "GET";request.CookieContainer = m_Cookie;//發送請求并獲取相應回應數據HttpWebResponse response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標網頁發送Post請求Stream responseStream = response.GetResponseStream();//創建寫入流MemoryStream stream = new MemoryStream();byte[] bArr = new byte[1024];int size = responseStream.Read(bArr, 0, (int)bArr.Length);while (size > 0){stream.Write(bArr, 0, size);size = responseStream.Read(bArr, 0, (int)bArr.Length);}stream.Seek(0, SeekOrigin.Begin);responseStream.Close();return stream;}#endregion#region HttpUploadFile 上傳文件/// <summary>/// Http上傳文件/// </summary>public static string HttpUploadFile(string url, byte[] bArr, string fileName){// 設置參數HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;CookieContainer cookieContainer = new CookieContainer();request.CookieContainer = cookieContainer;request.AllowAutoRedirect = true;request.Method = "POST";string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線request.ContentType = "text/plain;charset=utf-8";request.CookieContainer = m_Cookie;Stream postStream = request.GetRequestStream();postStream.Write(bArr, 0, bArr.Length);postStream.Close();//發送請求并獲取相應回應數據HttpWebResponse response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標網頁發送Post請求Stream instream = response.GetResponseStream();StreamReader sr = new StreamReader(instream, Encoding.UTF8);//返回結果網頁(html)代碼string content = sr.ReadToEnd();return content;}#endregion#region HttpPost/// <summary>/// HttpPost/// </summary>public static string HttpPost(string url, string data){byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data);// 設置參數HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;request.CookieContainer = m_Cookie;request.Method = "POST";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 = request.GetRequestStream();postStream.Write(bArr, 0, bArr.Length);postStream.Close();//發送請求并獲取相應回應數據HttpWebResponse response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標網頁發送Post請求Stream responseStream = response.GetResponseStream();//返回結果網頁(html)代碼MemoryStream memoryStream = new MemoryStream();bArr = new byte[1024];int size = responseStream.Read(bArr, 0, (int)bArr.Length);while (size > 0){memoryStream.Write(bArr, 0, size);size = responseStream.Read(bArr, 0, (int)bArr.Length);Thread.Sleep(1);}string content = Encoding.UTF8.GetString(memoryStream.ToArray());return content;}#endregion#region HttpPost/// <summary>/// HttpPost/// </summary>public static string HttpPost(string url){// 設置參數HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;request.CookieContainer = m_Cookie;request.Method = "POST";request.ContentType = "text/plain;charset=utf-8";request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";//發送請求并獲取相應回應數據HttpWebResponse response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標網頁發送Post請求Stream responseStream = response.GetResponseStream();//返回結果網頁(html)代碼MemoryStream memoryStream = new MemoryStream();byte[] bArr = new byte[1024];int size = responseStream.Read(bArr, 0, (int)bArr.Length);while (size > 0){memoryStream.Write(bArr, 0, size);size = responseStream.Read(bArr, 0, (int)bArr.Length);Thread.Sleep(1);}string content = Encoding.UTF8.GetString(memoryStream.ToArray());return content;}#endregion#region HttpGet/// <summary>/// HttpGet/// </summary>public static string HttpGet(string url){// 設置參數HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;request.CookieContainer = m_Cookie;request.Method = "GET";request.ContentType = "text/plain;charset=utf-8";request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";//發送請求并獲取相應回應數據HttpWebResponse response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標網頁發送Post請求Stream responseStream = response.GetResponseStream();//返回結果網頁(html)代碼MemoryStream memoryStream = new MemoryStream();byte[] bArr = new byte[1024];int size = responseStream.Read(bArr, 0, (int)bArr.Length);while (size > 0){memoryStream.Write(bArr, 0, size);size = responseStream.Read(bArr, 0, (int)bArr.Length);Thread.Sleep(1);}string content = Encoding.UTF8.GetString(memoryStream.ToArray());return content;}#endregion} } View Code2、Windows服務代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.ServiceProcess; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows.Forms; using Utils;namespace BugMonitor {public partial class BugMonitorService : ServiceBase{[DllImport("kernel32.dll", SetLastError = true)]public static extern int WTSGetActiveConsoleSessionId();[DllImport("wtsapi32.dll", SetLastError = true)]public static extern bool WTSSendMessage(IntPtr hServer,int SessionId,String pTitle,int TitleLength,String pMessage,int MessageLength,int Style,int Timeout,out int pResponse,bool bWait);public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;private System.Timers.Timer timer;private static List<int> idList = new List<int>();private string loginUrl = ConfigurationManager.AppSettings["loginUrl"];private string listUrl = ConfigurationManager.AppSettings["bugListUrl"];private string userLogin = ConfigurationManager.AppSettings["userName"];private string userPassword = ConfigurationManager.AppSettings["userPassword"];private Regex regTr = new Regex(@"<tr class=""listTableLine(?:(?!</tr>)[\s\S])*</tr>", RegexOptions.IgnoreCase);private Regex regTd = new Regex(@"<td align=""left"">((?:(?!</td>)[\s\S])*)</td>", RegexOptions.IgnoreCase);private int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pageSize"]);private double interval = Convert.ToDouble(ConfigurationManager.AppSettings["interval"]);private string projectId = ConfigurationManager.AppSettings["projectId"];public BugMonitorService(){InitializeComponent();}public static void ShowMessageBox(string message, string title){int resp = 0;WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,WTSGetActiveConsoleSessionId(),title, title.Length,message, message.Length,0, 0, out resp, false);}protected override void OnStart(string[] args){LogUtil.path = Application.StartupPath + "\\log";timer = new System.Timers.Timer(interval * 60 * 1000);timer.Elapsed += new System.Timers.ElapsedEventHandler(Action);timer.Start();LogUtil.Log("服務啟動成功");}protected override void OnStop(){if (timer != null){timer.Stop();timer.Close();timer.Dispose();timer = null;}LogUtil.Log("服務停止成功");Thread.Sleep(100); //等待一會,待日志寫入文件 }public void Start(){OnStart(null);}public void Action(object sender, ElapsedEventArgs e){try{Task.Factory.StartNew(() =>{try{int bugCount = 0;string loginResult = HttpUtil.HttpPost(loginUrl, string.Format("uer={0}&userPassword={1}&submit=%E7%99%BB%E5%BD%95&userLogin={0}&uer=", userLogin, userPassword));string result = HttpUtil.HttpPost(listUrl, string.Format("projectId={0}&perListDPF={1}&sortFieldDPF=bugCode&sortSequenceDPF=1&bugStatus=1", projectId, pageSize));ProcessBug(result, ref bugCount);result = HttpUtil.HttpPost(listUrl, string.Format("projectId={0}&perListDPF={1}&sortFieldDPF=bugCode&sortSequenceDPF=1&bugStatus=3", projectId, pageSize));ProcessBug(result, ref bugCount);if (bugCount > 0){ShowMessageBox(string.Format("您有 {0} 個新BUG", bugCount), "提醒");}else{LogUtil.Log("沒有新BUG");}}catch (Exception ex){LogUtil.LogError(ex.Message + "\r\n" + ex.StackTrace);}});}catch (Exception ex){LogUtil.LogError(ex.Message + "\r\n" + ex.StackTrace);}}private void ProcessBug(string bugListPageHtml, ref int bugCount){MatchCollection mcTr = regTr.Matches(bugListPageHtml);foreach (Match mTr in mcTr){MatchCollection mcTd = regTd.Matches(mTr.Value);if (mcTd.Count > 0){int id = Convert.ToInt32(mcTd[0].Groups[1].Value.Trim());string strStatus = mcTd[1].Value.ToLower();if (!idList.Exists(a => a == id)){if (strStatus.IndexOf("已激活") > 0 || strStatus.IndexOf("重新打開") > 0){idList.Add(id);bugCount++;LogUtil.Log(string.Format("發現新的BUG,BUG編號:{0}", id));}}}}}} } View Code?
轉載于:https://www.cnblogs.com/s0611163/p/7245781.html
總結
以上是生活随笔為你收集整理的写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Unity中水和雾的使用
- 下一篇: PHP CRC16 校验码的算法怎么使用