public static PostProjectInfo GetConfig(string ProjectName) { PostProjectInfo objPostProjectInfo = new PostProjectInfo(); objPostProjectInfo.BaseInfo = new BaseInfo(); objPostProjectInfo.UrlInfoList = new List<UrlInfo>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(MainForm.configFilePath);//加载文件 XmlNode xnProject = xmlDoc.SelectSingleNode(string.Format("/JyiConfig/PostProject/Item[@Name='{0}']", ProjectName));//查找节点 objPostProjectInfo.BaseInfo.Name=xnProject.Attributes["Name"].Value;//设置项目名称 XmlNode xnBase=xnProject.SelectSingleNode("Base");//寻找到Base配置节,开始读取基本配置 objPostProjectInfo.BaseInfo.Count = int.Parse(xnBase.SelectSingleNode("Count").InnerText);//执行周期次数 objPostProjectInfo.BaseInfo.Charset = xnBase.SelectSingleNode("Charset").InnerText;//页面编码 objPostProjectInfo.BaseInfo.UseProxy = int.Parse(xnBase.SelectSingleNode("UseProxy").InnerText);//是否使用代理 objPostProjectInfo.BaseInfo.UseCookie = int.Parse(xnBase.SelectSingleNode("UseCookie").InnerText);//是否使用Cookie objPostProjectInfo.BaseInfo.ClearCookieEverytime = int.Parse(xnBase.SelectSingleNode("ClearCookieEverytime").InnerText);//每个周期循环前是否清除Cookie objPostProjectInfo.BaseInfo.WaitTime = int.Parse(xnBase.SelectSingleNode("WaitTime").InnerText);//每个周期间隔时间(毫秒) //寻找到Url配置节,开始读取配置 XmlNodeList xnlUrl = xnProject.SelectSingleNode("Url").ChildNodes; foreach (XmlNode xnUrlItem in xnlUrl)//循环读取每一个Item节点 { //XmlNode xnUrl = xnProject.SelectSingleNode("Url"); UrlInfo objUrlInfo = new UrlInfo(); objUrlInfo.Name = xnUrlItem.Attributes["Name"].Value;//此Url的代号名称 objUrlInfo.Url = xnUrlItem.Attributes["Url"].Value;//此Url的地址 objUrlInfo.Count = int.Parse(xnUrlItem.SelectSingleNode("Count").InnerText);//Url提交次数 objUrlInfo.WaitTime = int.Parse(xnUrlItem.SelectSingleNode("WaitTime").InnerText);//Url提交间隔时间(毫秒) objUrlInfo.Referer = xnUrlItem.SelectSingleNode("Referer").InnerText; objUrlInfo.SuccessText = xnUrlItem.SelectSingleNode("SuccessText").InnerText;//成功标志 objUrlInfo.Method = xnUrlItem.SelectSingleNode("Method").InnerText;//提交方式 objUrlInfo.HavePostParms = int.Parse(xnUrlItem.SelectSingleNode("HavePostParms").InnerText);//是否有Post提交参数 if (objUrlInfo.HavePostParms > 0)//如果有Post提交参数,加载PostParms列表 { objUrlInfo.PostParmsList = new List<ParmsInfo>();//初始化PostParms参数泛型列表 XmlNodeList xnlPostParms = xnUrlItem.SelectSingleNode("PostParms").ChildNodes; foreach (XmlNode xnf in xnlPostParms) { ParmsInfo objParm = new ParmsInfo(); XmlElement xe = (XmlElement)xnf; objParm.Name = xnf.InnerText; objParm.Type = System.Web.HttpUtility.UrlEncode(xnf.Attributes["Type"].Value, Encoding.GetEncoding(objPostProjectInfo.BaseInfo.Charset)); objUrlInfo.PostParmsList.Add(objParm); } } objUrlInfo.UseVCode = int.Parse(xnUrlItem.SelectSingleNode("UseVCode").InnerText);//是否使用验证码 if (objUrlInfo.UseVCode > 0)//加载验证码识别列表 { objUrlInfo.UnCodeList = new List<UnCodeInfo>();//初始化UnCode参数泛型列表 objUrlInfo.VCodeUrl = xnUrlItem.SelectSingleNode("UnCode").Attributes["Url"].Value;//验证码图片地址 XmlNodeList xnlUnCode = xnUrlItem.SelectSingleNode("UnCode").ChildNodes; foreach (XmlNode xnf in xnlUnCode) { UnCodeInfo objUnCode = new UnCodeInfo(); XmlElement xe = (XmlElement)xnf; objUnCode.ImgCode = xnf.InnerText;//图片码 objUnCode.Code = xnf.Attributes["Code"].Value;//实际码 objUrlInfo.UnCodeList.Add(objUnCode); } } objUrlInfo.RemoveParms = xnUrlItem.SelectSingleNode("RemoveParms").InnerText.Trim();//需要移除的预处理参数,移除本次Url使用后别人不再使用的预处理参数,避免在后面的Url的Post中遇到重名的参数被错误Post objUrlInfo.HaveGetParms = int.Parse(xnUrlItem.SelectSingleNode("HaveGetParms").InnerText);//是否有GetParms预处理参数 if (objUrlInfo.HaveGetParms > 0)//如果有预处理的Parms { objUrlInfo.GetParmsList = new List<GetParmsInfo>();//初始化GetParms参数泛型列表 XmlNodeList xnlUnCode = xnUrlItem.SelectSingleNode("GetParms").ChildNodes; foreach (XmlNode xnf in xnlUnCode) { GetParmsInfo objGetParmsInfo = new GetParmsInfo(); XmlElement xe = (XmlElement)xnf; objGetParmsInfo.Name = xnf.InnerText;//预处理参数名 objGetParmsInfo.Regex = xnf.Attributes["Regex"].Value;//分析的正则 objUrlInfo.GetParmsList.Add(objGetParmsInfo); } } objPostProjectInfo.UrlInfoList.Add(objUrlInfo);//循环完毕一个Url的Item节点,加入列表 继续循环. } return objPostProjectInfo; }
public void ExecuteProject() { m_MainForm.SetText(string.Format("项目『{0}』启动线程成功~\r\n", m_ProjectName)); objPostProjectInfo = PostProjectConfig.GetConfig(m_ProjectName);//初始化项目实例 objBaseConfigInfo = BaseConfig.GetBaseConfig();//初始化基本配置类,目前代理列表在其中 cookieContainer = new CookieContainer();//初始化Cookie ProxyInfo objProxyInfo;//声明代理信息对象 WebProxy objWebProxy=null;//声明代理对象 //需要使用的预处理参数列表 List<ParmsInfo> objGetParmsInfoList = new List<ParmsInfo>(); for (int i = 0; i < objPostProjectInfo.BaseInfo.Count; i++) { urlThreadSuccessFlag = 1;//执行Url任务时候,如果为0,就跳出当前Url列表,重新开始新的列表.默认为成功(1,由于会在后面被重置为0,每个列表开始循环是先初始化为1,否则后面无法进行) if (objPostProjectInfo.BaseInfo.ClearCookieEverytime > 0)//如果每个周期循环前都需要清除Cookie { cookieContainer = new CookieContainer(); } if (objPostProjectInfo.BaseInfo.UseCookie <= 0) { cookieContainer = null;//如果cookie为空,在conn类中将自动判断为不使用cookie } objWebProxy = null;//初始化代理对象为空(因为如果代理为空,在conn类中将自动判断为不使用代理.如果要使用代理,则通过下面的语句赋值) if (objPostProjectInfo.BaseInfo.UseProxy > 0) { objProxyInfo = Proxy.GetNextProxy(objBaseConfigInfo.ProxyList);//获取未使用过的代理信息对象 if (objProxyInfo != null)//如果配置为需要使用代理且代理没有循环完,则赋值给代理对象,但是代理却用完了,则终止 { objWebProxy = new WebProxy(objProxyInfo.Address, objProxyInfo.Port); } else { m_MainForm.SetText("无可用代理,自动停止项目执行!\r\n"); Utility.Utility.WriteGoodProxyListToXml(objGoodProxyInfoList);//写入goodproxy Thread.CurrentThread.Abort(); } } //开始处理Url列表并链接 foreach (UrlInfo objUrlInfo in objPostProjectInfo.UrlInfoList) { if (urlThreadSuccessFlag == 0) { m_MainForm.SetText(string.Format("\r\n===========在上个Url连接中失败,重新开始新一轮的列表循环===========\r\n")); //continue; break; } m_MainForm.SetText(string.Format("Url:[{0}]开始\r\n", objUrlInfo.Name)); for (int j = 0; j < objUrlInfo.Count; j++)//目前不管Count是多少还只能使用一个代理连接,想到的解决方案是,拷贝一份objProxyInfoList,然后再当前Url里面循环 { if (objUrlInfo.Method == "POST") { StringBuilder sbParms = new StringBuilder(); foreach (ParmsInfo objPostParmsInfo in objUrlInfo.PostParmsList)//目前Url参数全部post,然后从objGetParmsList中得到预处理参数post { if (objPostParmsInfo.Type == "Rand") { sbParms.Append(string.Format("&{0}={1}", objPostParmsInfo.Name, Utility.Utility.GenerateRandom(8,Jyi.Utility.Utility.RandomType.Lowercased))); } else if (objPostParmsInfo.Type == "RandEmail") { sbParms.Append(string.Format("&{0}={1}@qq.com", objPostParmsInfo.Name, Utility.Utility.GenerateRandom(8))); } else if (objPostParmsInfo.Type == "VCode") { #region 得到验证码 HttpWebRequest objImgRequest = (HttpWebRequest)HttpWebRequest.Create(objUrlInfo.VCodeUrl); objImgRequest.Method = "GET"; objImgRequest.CookieContainer = cookieContainer; if (objWebProxy != null) { objImgRequest.Proxy = objWebProxy; } WebResponse wr2 = objImgRequest.GetResponse(); System.IO.Stream s = wr2.GetResponseStream(); System.Drawing.Image numPic = System.Drawing.Image.FromStream(s);// 得到验证码图片 UnCode objUnCode = new UnCode((System.Drawing.Bitmap)numPic); #endregion sbParms.Append(string.Format("&{0}={1}", objPostParmsInfo.Name, objUnCode.getPicnum(objUrlInfo.UnCodeList))); } else if (objPostParmsInfo.Type == "PreParm") { foreach (ParmsInfo objGetParms in objGetParmsInfoList) { if (objPostParmsInfo.Name == objGetParms.Name) { sbParms.Append(string.Format("&{0}={1}", objPostParmsInfo.Name, objGetParms.Type)); m_MainForm.SetText(string.Format("取得预处理参数:{0}={1}\r\n", objPostParmsInfo.Name, objGetParms.Type)); } } } else { sbParms.Append(string.Format("&{0}={1}", objPostParmsInfo.Name, objPostParmsInfo.Type)); } } //执行RemoveParms,移除本次使用后别人不再使用的预处理参数,避免在后面的Url的Post中遇到重名的参数被错误Post if (objUrlInfo.RemoveParms != "") { int listCount = objGetParmsInfoList.Count; //copy副本 //List<ParmsInfo> objGetParmsInfoListTemp = new List<ParmsInfo>() ; //foreach (ParmsInfo objGetParmInfo in objGetParmsInfoList) //{ // ParmsInfo objCopyGetParmsInfo = new ParmsInfo(); // objCopyGetParmsInfo.Name = objGetParmInfo.Name; // objCopyGetParmsInfo.Type = objGetParmInfo.Type; // objGetParmsInfoListTemp.Add(objCopyGetParmsInfo); //} foreach (string strRemoveParm in objUrlInfo.RemoveParms.Split(',')) { for (int k = 0; k < listCount; k++) { if (objGetParmsInfoList[k].Name == strRemoveParm) { objGetParmsInfoList.Remove(objGetParmsInfoList[k]); m_MainForm.SetText(string.Format("移除预处理参数{0}\r\n", strRemoveParm)); } } //foreach (ParmsInfo objGetParms in objGetParmsInfoListTemp) //{ // if (objGetParms.Name == strRemoveParm) // { // objGetParmsInfoList.Remove(objGetParms); // m_MainForm.SetText(string.Format("移除预处理参数{0}\r\n", objGetParms.Name)); // } //} } } m_MainForm.SetText(string.Format("(POST){0}\r\n", objUrlInfo.Url)); //POST数据 string returnData = Conn.PostData(objUrlInfo.Url, objPostProjectInfo.BaseInfo.Charset, sbParms.ToString(), objUrlInfo.Referer, 0, objWebProxy, cookieContainer); if (returnData.IndexOf("Jyi链接失败")>=0) { m_MainForm.SetText("链接失败!\r\n"); m_MainForm.SetText(string.Format("===\r\n{0}\r\n===\r\n", returnData)); //Thread.CurrentThread.Abort(); urlThreadSuccessFlag = 0; //continue; break; } else if (returnData.IndexOf(objUrlInfo.SuccessText) >= 0) { m_MainForm.SetText("(POST)成功!\r\n"); urlThreadSuccessFlag = 1; } else { m_MainForm.SetText("(POST)失败!\r\n"); m_MainForm.SetText(string.Format("===\r\n{0}\r\n===\r\n", returnData)); //Thread.CurrentThread.Abort(); urlThreadSuccessFlag = 0; //continue; break; } //得到预处理参数,添加到预处理列表中 if (objUrlInfo.HaveGetParms > 0) { foreach (GetParmsInfo objGetParm in objUrlInfo.GetParmsList) { ParmsInfo objAddGetParm = new ParmsInfo(); objAddGetParm.Name = objGetParm.Name; RegexFunc rFunc = new RegexFunc(); objAddGetParm.Type = System.Web.HttpUtility.UrlEncode(rFunc.GetMatch(returnData, objGetParm.Regex), Encoding.GetEncoding(objPostProjectInfo.BaseInfo.Charset)); objGetParmsInfoList.Add(objAddGetParm); m_MainForm.SetText(string.Format("获取参数{0}的值为{1}\r\n", objAddGetParm.Name, objAddGetParm.Type)); } } m_MainForm.SetText(string.Format("(POST)结束\r\n")); Thread.Sleep(objUrlInfo.WaitTime);//Url间隔时间 } else { //TODO GETDATA TODAY m_MainForm.SetText(string.Format("(GET){0}\r\n", objUrlInfo.Url)); //POST数据 string returnData = Conn.GetData(objUrlInfo.Url, objPostProjectInfo.BaseInfo.Charset, objUrlInfo.Referer, 0, objWebProxy, cookieContainer); if (returnData.IndexOf("Jyi链接失败") >= 0) { m_MainForm.SetText("链接失败!\r\n"); m_MainForm.SetText(string.Format("===\r\n{0}\r\n===\r\n", returnData)); //Thread.CurrentThread.Abort(); urlThreadSuccessFlag = 0; //continue; break; } else if (returnData.IndexOf(objUrlInfo.SuccessText) >= 0) { m_MainForm.SetText("(GET)成功!\r\n"); urlThreadSuccessFlag = 1; } else { m_MainForm.SetText("(GET)失败!\r\n"); m_MainForm.SetText(string.Format("===\r\n{0}\r\n===\r\n", returnData)); //Thread.CurrentThread.Abort(); urlThreadSuccessFlag = 0; //continue; break; } if (objUrlInfo.HaveGetParms > 0) { //得到预处理参数,添加到预处理列表中 foreach (GetParmsInfo objGetParm in objUrlInfo.GetParmsList) { ParmsInfo objAddGetParm = new ParmsInfo(); objAddGetParm.Name = objGetParm.Name; RegexFunc rFunc = new RegexFunc(); objAddGetParm.Type = System.Web.HttpUtility.UrlEncode(rFunc.GetMatch(returnData, objGetParm.Regex), Encoding.GetEncoding(objPostProjectInfo.BaseInfo.Charset)); objGetParmsInfoList.Add(objAddGetParm); m_MainForm.SetText(string.Format("获取参数{0}的值为{1}\r\n", objAddGetParm.Name, objAddGetParm.Type)); } } m_MainForm.SetText(string.Format("(GET)结束\r\n")); Thread.Sleep(objUrlInfo.WaitTime);//Url间隔时间 } } m_MainForm.SetText(string.Format("Url:[{0}]完毕\r\n", objUrlInfo.Name)); } if (urlThreadSuccessFlag == 1&&objWebProxy!=null) { ProxyInfo objGoodProxyInfo = new ProxyInfo(); objGoodProxyInfo.Address = objWebProxy.Address.Host; objGoodProxyInfo.Port = objWebProxy.Address.Port; objGoodProxyInfo.Name = "GoodProxy"; objGoodProxyInfoList.Add(objGoodProxyInfo); } m_MainForm.SetText(string.Format("项目『{0}』线程完成一次循环~\r\n", m_ProjectName)); Thread.Sleep(objPostProjectInfo.BaseInfo.WaitTime);//项目间隔时间 } if (objWebProxy != null) { Utility.Utility.WriteGoodProxyListToXml(objGoodProxyInfoList);//写入goodproxy } m_MainForm.SetText(string.Format("项目『{0}』成功完成任务~\r\n", m_ProjectName)); //TODO:处理点X关闭的时候自动结束线程,避免线程访问窗体出错 }