写了一个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 Code

2、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,并提醒我...相关推荐

  1. C#模拟网站用户登录

    我们在写灌水机器人.抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录.那么怎么用C#来模拟一个用户的登录拉?要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的. ...

  2. 自学爬虫项目(二)一一利用selenium模拟淘宝登录,爬取商品数据

    文章目录 前言 一.明确目标 二.分析过程 三.代码封装 总结 前言 你是否还在为学习Python没有方向而苦恼?快来跟着壹乐一起学习吧!让我们共同进步! 今天我们用selenium与Beautifu ...

  3. C#创建、安装一个Windows服务

    关于WIndows服务的介绍,之前写过一篇:http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务不 ...

  4. 创建一个windows服务的小程序及注意事项

    1,首先在vs中创建一个windows服务项目 会生成一个Service1.cs的文件  打开该文件 切换到代码视图  有两个方法   OnStart(string[] args)和OnStop()方 ...

  5. 新建第一个windows服务(Windows Service)

    首先,请原谅我是一个小白,一直到前段时间才在工作需要的情况下写了第一个windows服务.首先说一下为什么写这个windows服务吧,也就是什么需求要我来写这么一个东西.公司的项目中,需要一个预警功能 ...

  6. C# 建一个Windows 服务 定时发邮件

    1.打开VS建一个Windows 服务 2.下一步,填好项目名称和项目保存的地址 3.创建之后,右击.选择添加安装程序 4.添加安装程序之后会出现'serviceInstaller1'=>在此控 ...

  7. C# 如何创建一个Windows服务(Windows Service)

    Windows服务经常用来做一些定时任务处理,今天来说一下如何搭建一个Windows服务(基础篇,不喜勿喷). 1.搭建一个Windows Servier,我是VS2017 .NET FrameWor ...

  8. windows7未能连接一个windows服务(无法连接网络)的解决方法

    今天下午不知道怎么搞的,就搞的无线不可以用了,出现了个  "windows7未能连接一个windows服务"问题,而且连eclipse都打不开了. 晚上在网上找了一个方法,终于搞定 ...

  9. 电脑显示未连接一个服务器怎么处理,win7系统提示未能连接一个windows服务如何解决【详解】...

    虽然win10系统已经出来很久了,不过还是有很多用选择使用win7系统,它的兼容性吸引了广大用户们,不过问题也有很多,最近有位win7系统用户使用电脑的时候,系统出现提示:未能连接一个windows服 ...

最新文章

  1. 厉害了,用Python实现自动扫雷!
  2. 华硕主板X99-E WS/USB 3.1 Intel Realsense D435摄像头掉线是否与Intel推行的xhci有关?
  3. boost::python::wrapper相关的测试程序
  4. jenkins java反序列化_Jenkins “Java 反序列化”过程远程命令执行漏洞
  5. 武汉大学计算机学院2019考研复试,2019年武汉大学硕士研究生复试及录取名单汇总...
  6. 线性表实现一元多项式操作
  7. RIP 图形、图像解析器
  8. 法拉利杀手Koenigsegg CCX
  9. Prompt Learning | 一文带你概览Prompt工作新进展
  10. QT学习之路:从入门到精通
  11. python微信机器人制作教程+源码
  12. PSP3000购机心得
  13. 20172328的结对编程四则运算第二周-整体总结
  14. word里面搜狗输入法突然不见了
  15. 一步一步实现STM32-FOTA系列教程之BIN文件解包C语言实现
  16. UE4.26像素流公网访问linux和win两种实现方式
  17. 3dmax:3dmax的软件两大常用工具之基本三维实体(标准基本体、扩展基本体、复合对象)之详细攻略
  18. 数据提取-数据提取软件
  19. [论文阅读]Spatio-Temporal Graph Routing for Skeleton-Based Action Recognition
  20. Linux命令之复制文件或目录cp

热门文章

  1. 三菱e68系统程序传输_盘点那些年用过的数控操作系统,全会操作的话你肯定是老师傅...
  2. v8 编译 linux,安装与编译 Javascript V8 Engine
  3. java查看sql视图_SQL视图与MS Access查询
  4. python与java的比较_Python和Java两者有什么区别?
  5. python-tkinter模块图形分布移动(可键盘操作)
  6. 笔记:Matrix completion by Truncated Nuclear Norm Regularization
  7. 【项目实战课】基于Pytorch的StyleGAN v1人脸图像生成实战
  8. 全球与中国塑料废料粉碎机市场运营状况分析及投资风险评估报告2022-2027年版
  9. 开放中国农业-国际农民丰收节贸易会:谋定全球共同发展
  10. ubuntu系统阅读CHM文档的最终解决方案