写了一个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[];
int size = responseStream.Read(bArr, , (int)bArr.Length);
while (size > )
{
stream.Write(bArr, , size);
size = responseStream.Read(bArr, , (int)bArr.Length);
}
stream.Seek(, 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, , 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, , 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[];
int size = responseStream.Read(bArr, , (int)bArr.Length);
while (size > )
{
memoryStream.Write(bArr, , size);
size = responseStream.Read(bArr, , (int)bArr.Length);
Thread.Sleep();
}
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[];
int size = responseStream.Read(bArr, , (int)bArr.Length);
while (size > )
{
memoryStream.Write(bArr, , size);
size = responseStream.Read(bArr, , (int)bArr.Length);
Thread.Sleep();
}
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[];
int size = responseStream.Read(bArr, , (int)bArr.Length);
while (size > )
{
memoryStream.Write(bArr, , size);
size = responseStream.Read(bArr, , (int)bArr.Length);
Thread.Sleep();
}
string content = Encoding.UTF8.GetString(memoryStream.ToArray());
return content;
}
#endregion }
}

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 = ;
WTSSendMessage(
WTS_CURRENT_SERVER_HANDLE,
WTSGetActiveConsoleSessionId(),
title, title.Length,
message, message.Length,
, , out resp, false);
} protected override void OnStart(string[] args)
{
LogUtil.path = Application.StartupPath + "\\log"; timer = new System.Timers.Timer(interval * * );
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(); //等待一会,待日志写入文件
} public void Start()
{
OnStart(null);
} public void Action(object sender, ElapsedEventArgs e)
{
try
{
Task.Factory.StartNew(() =>
{
try
{
int bugCount = ;
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 > )
{
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 > )
{
int id = Convert.ToInt32(mcTd[].Groups[].Value.Trim());
string strStatus = mcTd[].Value.ToLower();
if (!idList.Exists(a => a == id))
{
if (strStatus.IndexOf("已激活") > || strStatus.IndexOf("重新打开") > )
{
idList.Add(id);
bugCount++;
LogUtil.Log(string.Format("发现新的BUG,BUG编号:{0}", id));
}
}
}
}
}
}
}

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

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

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

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

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

  3. 创建第一个windows服务

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志. 计算机启动时,服务会自动开 ...

  4. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  5. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  6. 写了一个Windows API Viewer,提供VBA语句的导出功能。提供两万多个API的MSDN链接内容的本地查询

    始出处:http://www.cnblogs.com/Charltsing/p/APIViewer.html QQ:564955427,QQ群:550672198 世面上的API Viewer已经不少 ...

  7. 今天我自己第一次写了一个Windows批处理bat脚本,一起学习一下吧。

    今天我自己第一次写了一个Windows批处理bat脚本,备注一下 事情原由:自己使用Java开发了一个加解密的工具.但是当把工具给别人使用的时候,别人还需要把代码编译打包, 然后还需要看一下代码里面的 ...

  8. 用python写一个豆瓣短评通用爬虫(登录、爬取、可视化)

    原创技术公众号:bigsai,本文在1024发布,祝大家节日快乐,心想事成. @ 目录 前言 登录 爬取 储存 可视化分析 前言 在本人上的一门课中,老师对每个小组有个任务要求,介绍和完成一个小模块. ...

  9. python入门:模拟简单用户登录(自写)

    #!/usr/bin/env python # -*- coding: utf-8 -*- #模拟简单用户登录(自写) import getpass a = raw_input("Pleas ...

随机推荐

  1. 安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法

    安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法 原创 2015年07月31日 ...

  2. Java 8 字符串日期排序

    public class ObjectDto implements Serializable { private static final long serialVersionUID = 858983 ...

  3. Python time、datetime

    简介: 记录一下 Python 如何获取昨天.今天.明天时间及格式化. 1.今天 In [1]: import time In [2]: print time.strftime('%Y.%m.%d', ...

  4. java编写binder服务实例

    文件目录结果如下: 一. 编写AIDL文件 IHelloService.aidl: /** {@hide} */ interface IHelloService { void sayhello(); ...

  5. 迷你MVVM框架 avalonjs 学习教程6、插入移除处理

    ms-if是属于流程绑定的一种,如果表达式为真值那么就将当前元素输出页面,不是就将它移出DOM树.它的效果与上一章节的ms-visible效果看起来相似的,但它会影响到:empty伪类,并能更节约性能 ...

  6. 如何用MaskBlt实现两个位图的合并,从而实现背景透明

    我有两个位图,一个前景图,一个背景图(mask用途).请问如何用MaskBlt实现两个位图的合并,从而实现背景透明! 核心代码:dcImage.SetBkColor(crColour);dcMask. ...

  7. Linux学习---linux系统下安装配置Jenkins

    1.首先准备java环境,安装JDK 2.下载jenkins至Linux服务器 下载地址:https://wiki.jenkins-ci.org/display/JENKINS/Installing+ ...

  8. 启动JAR Hadoop任务

    [启动JAR Hadoop任务] 一般情况下,我们会使用下面的命令来运行一个hadoop任务: 这个命令实际上是转化成下面的命令来运行的 在RunJar中,会读取abc.jar文件,然后尝试从mani ...

  9. sed的基础应用

    sed是一个非交互式的文本编辑器:sed一行一行的处理文件 sed有模式空间(主要活动空间)和缓存空间(辅助空间)两个空间: 模式空间(pattern space)将文件中的一行内容读取到临时缓冲区( ...

  10. oracle 知识点

    1.条件运算2.关联运算,子查询3.集合运算4.函数运算5.分组运算[group by](凑维度,条件,过滤,分组函数)6.行转列7.PL/SQL