public static BaseConfigInfo GetBaseConfig() { BaseConfigInfo objBaseConfig = new BaseConfigInfo(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(MainForm.configFilePath); XmlNode xnBaseConfig = xmlDoc.SelectSingleNode("/JyiConfig"); #region Proxy List<Natsuhime.Proxy.ProxyInfo> list = Natsuhime.Proxy.ProxyUtility.GetProxyList_FromConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ProxyList.dat")); List<ProxyInfo> objProxyList = new List<ProxyInfo>(); foreach (Natsuhime.Proxy.ProxyInfo info in list) { ProxyInfo pi = new ProxyInfo(); pi.Name = info.Name; pi.Address = info.Address; pi.Port = info.Port; objProxyList.Add(pi); } //XmlNodeList xnlProxys = xnBaseConfig.SelectSingleNode("Proxy").ChildNodes; //List<ProxyInfo> objProxyList = new List<ProxyInfo>(); //foreach (XmlNode xnf in xnlProxys) //{ // ProxyInfo objProxy = new ProxyInfo(); // XmlElement xe = (XmlElement)xnf; // objProxy.Name = xnf.InnerText; // objProxy.Address = xnf.Attributes["Address"].Value; // objProxy.Port = int.Parse(xnf.Attributes["Port"].Value); // objProxyList.Add(objProxy); //} objBaseConfig.ProxyList = objProxyList; #endregion return objBaseConfig; }
private void btnGetProxy_Click(object sender, EventArgs e) { objProxyForm = this; List<ProxyGetInfo> objProxyGetList = new List<ProxyGetInfo>(); objProxyGetList = ProxyGetConfig.GetProxyGetPageUrlList(); string returnData; foreach (ProxyGetInfo objProxyGetInfo in objProxyGetList) { objProxyForm.SetText(string.Format("Url:{0}分析开始\r\n",objProxyGetInfo.PageUrl)); returnData = Conn.GetData(objProxyGetInfo.PageUrl, objProxyGetInfo.Charset,objProxyGetInfo.PageUrl,10000,null,null); if (cbxDebugMode.Checked == true) { tbMessage.Text += string.Format("{0}\r\n", returnData); } else { RegexFunc rFunc = new RegexFunc(); System.Text.RegularExpressions.MatchCollection m = rFunc.GetMatchFull(returnData, objProxyGetInfo.Regex); List<ProxyInfo> objProxyList = new List<ProxyInfo>(); foreach (System.Text.RegularExpressions.Match objMatch in m) { ProxyInfo objProxyInfo = new ProxyInfo(); objProxyInfo.Address = objMatch.Groups[1].Value.Split(':')[0]; objProxyInfo.Port = int.Parse(objMatch.Groups[1].Value.Split(':')[1]); objProxyInfo.Name = "暂无"; objProxyList.Add(objProxyInfo); tbMessage.Text += string.Format("获得地址:{0}:{1}\r\n", objProxyInfo.Address, objProxyInfo.Port); } Utility.Utility.WriteProxyListToXml(objProxyList); } } }
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关闭的时候自动结束线程,避免线程访问窗体出错 }