CommonHelper.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. using FCS.Models;
  2. using FCS.Models.DTO;
  3. using HtmlAgilityPack;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Xml;
  18. namespace FCS.Common
  19. {
  20. /// <summary>
  21. /// 公用帮助类
  22. /// </summary>
  23. public static class CommonHelper
  24. {
  25. /// <summary>
  26. /// 将XML内容转换成目标对象实体集合
  27. /// </summary>
  28. /// <typeparam name="T">目标对象实体</typeparam>
  29. /// <param name="FileName">完整文件名(根目录下只需文件名称)</param>
  30. /// <param name="WrapperNodeName"></param>
  31. /// <returns></returns>
  32. public static List<T> ConvertXMLToObject<T>(string FileName, string WrapperNodeName)
  33. {
  34. XmlDocument doc = new XmlDocument();
  35. doc.Load(FileName);
  36. List<T> result = new List<T>();
  37. var TType = typeof(T);
  38. XmlNodeList nodeList = doc.ChildNodes;
  39. if (!string.IsNullOrEmpty(WrapperNodeName))
  40. {
  41. foreach (XmlNode node in doc.ChildNodes)
  42. {
  43. if (node.Name == WrapperNodeName)
  44. {
  45. nodeList = node.ChildNodes;
  46. break;
  47. }
  48. }
  49. }
  50. object oneT = null;
  51. foreach (XmlNode node in nodeList)
  52. {
  53. if (node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.XmlDeclaration) continue;
  54. oneT = TType.Assembly.CreateInstance(TType.FullName);
  55. foreach (XmlNode item in node.ChildNodes)
  56. {
  57. if (item.NodeType == XmlNodeType.Comment) continue;
  58. var property = TType.GetProperty(item.Name);
  59. if (property != null)
  60. property.SetValue(oneT, Convert.ChangeType(item.InnerText, property.PropertyType), null);
  61. }
  62. result.Add((T)oneT);
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// 从作业数据地图中获取配置信息
  68. /// </summary>
  69. /// <param name="datamap">作业数据地图</param>
  70. /// <returns></returns>
  71. public static FCSConfig GetConfigFromDataMap(JobDataMap datamap)
  72. {
  73. FCSConfig config = new FCSConfig();
  74. var properties = typeof(FCSConfig).GetProperties();
  75. foreach (PropertyInfo info in properties)
  76. {
  77. if (info.PropertyType == typeof(string))
  78. info.SetValue(config, datamap.GetString(info.Name), null);
  79. else if (info.PropertyType == typeof(Int32))
  80. info.SetValue(config, datamap.GetInt(info.Name), null);
  81. }
  82. return config;
  83. }
  84. #region 日志信息
  85. public static string GetJobMainLogInfo(string QiHao)
  86. {
  87. return string.Format("通过主站地址抓取{0}期开奖数据成功", QiHao);
  88. }
  89. public static string GetJobLogError(string QiHao)
  90. {
  91. return string.Format("【{0}】抓取期开奖数据失败", QiHao);
  92. }
  93. #endregion 日志信息
  94. /// <summary>
  95. /// 将值转换为T类型数据
  96. /// </summary>
  97. /// <typeparam name="T">目标类型</typeparam>
  98. /// <param name="value">数据值</param>
  99. /// <returns></returns>
  100. public static T ChangeType<T>(object value)
  101. {
  102. return ChangeType<T>(value, default(T));
  103. }
  104. /// <summary>
  105. /// 将值转换为T类型数据,失败则返回T类型默认值
  106. /// </summary>
  107. /// <typeparam name="T">目标类型</typeparam>
  108. /// <param name="value">数据值</param>
  109. /// <param name="defaultValue">T类型默认值</param>
  110. /// <returns></returns>
  111. public static T ChangeType<T>(object value, T defaultValue)
  112. {
  113. if (value != null)
  114. {
  115. Type nullableType = typeof(T);
  116. if (!nullableType.IsInterface && (!nullableType.IsClass || (nullableType == typeof(string))))
  117. {
  118. if (nullableType.IsGenericType && (nullableType.GetGenericTypeDefinition() == typeof(Nullable<>)))
  119. {
  120. return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(nullableType));
  121. }
  122. if (nullableType.IsEnum)
  123. {
  124. return (T)Enum.Parse(nullableType, value.ToString());
  125. }
  126. return (T)Convert.ChangeType(value, nullableType);
  127. }
  128. if (value is T)
  129. {
  130. return (T)value;
  131. }
  132. }
  133. return defaultValue;
  134. }
  135. /// <summary>
  136. /// 将值转换为type类型的值
  137. /// </summary>
  138. /// <param name="value">值</param>
  139. /// <param name="type">目标类型</param>
  140. /// <returns></returns>
  141. public static object ChangeType(object value, Type type)
  142. {
  143. if (value != null)
  144. {
  145. var nullableType = Nullable.GetUnderlyingType(type);
  146. if (nullableType != null)//可空
  147. {
  148. return Convert.ChangeType(value, nullableType);
  149. }
  150. if (Convert.IsDBNull(value))//特殊处理,由于数据库类型与项目中的类型定义不匹配
  151. return type.IsValueType ? Activator.CreateInstance(type) : null;
  152. return Convert.ChangeType(value, type);
  153. }
  154. return null;
  155. }
  156. #region 获取全局唯一GUID
  157. /// <summary>
  158. /// 获取全局唯一GUID
  159. /// </summary>
  160. /// <param name="needReplace">是否需要替换-</param>
  161. /// <param name="format">格式化</param>
  162. /// <example>N:38bddf48f43c48588e0d78761eaa1ce6</example>>
  163. /// <example>P:(778406c2-efff-4262-ab03-70a77d09c2b5)</example>>
  164. /// <example>B:{09f140d5-af72-44ba-a763-c861304b46f8}</example>>
  165. /// <example>D:57d99d89-caab-482a-a0e9-a0a803eed3ba</example>>
  166. /// <returns></returns>
  167. public static string GetGuid(bool needReplace = true, string format = "N")
  168. {
  169. Guid res = NewSequentialGuid();//Guid.NewGuid();
  170. return needReplace ? res.ToString(format) : res.ToString();
  171. }
  172. [System.Runtime.InteropServices.DllImport("rpcrt4.dll", SetLastError = true)]
  173. static extern int UuidCreateSequential(byte[] buffer);
  174. /// <summary>
  175. /// 创建有序GUID
  176. /// </summary>
  177. /// <returns></returns>
  178. private static Guid NewSequentialGuid()
  179. {
  180. byte[] raw = new byte[16];
  181. if (UuidCreateSequential(raw) != 0)
  182. throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
  183. byte[] fix = new byte[16];
  184. // reverse 0..3
  185. fix[0x0] = raw[0x3];
  186. fix[0x1] = raw[0x2];
  187. fix[0x2] = raw[0x1];
  188. fix[0x3] = raw[0x0];
  189. // reverse 4 & 5
  190. fix[0x4] = raw[0x5];
  191. fix[0x5] = raw[0x4];
  192. // reverse 6 & 7
  193. fix[0x6] = raw[0x7];
  194. fix[0x7] = raw[0x6];
  195. // all other are unchanged
  196. fix[0x8] = raw[0x8];
  197. fix[0x9] = raw[0x9];
  198. fix[0xA] = raw[0xA];
  199. fix[0xB] = raw[0xB];
  200. fix[0xC] = raw[0xC];
  201. fix[0xD] = raw[0xD];
  202. fix[0xE] = raw[0xE];
  203. fix[0xF] = raw[0xF];
  204. return new Guid(fix);
  205. }
  206. #endregion 获取全局唯一GUID
  207. #region HtmlAgilityPack
  208. public static int IpCount;
  209. public static List<string> IpList;
  210. public static string GetIp(string path = "", bool isGetIp = true)
  211. {
  212. if (isGetIp && IpList.Count > 0)
  213. {
  214. var ran = new Random().Next(0, IpList.Count);
  215. return IpList[ran];
  216. }
  217. if (path.IsEmpty())
  218. path = AppDomain.CurrentDomain.BaseDirectory + "/XmlConfig/IP.txt";
  219. StreamReader sr;
  220. try
  221. {
  222. sr = new StreamReader(path, System.Text.Encoding.GetEncoding("utf-8"));
  223. }
  224. catch (Exception)
  225. {
  226. Thread.Sleep(1000);
  227. sr = new StreamReader(path, System.Text.Encoding.GetEncoding("utf-8"));
  228. }
  229. string content = sr.ReadToEnd().ToString();
  230. sr.Close();
  231. var list = content.JsonToList<string>();
  232. content = "";
  233. if (isGetIp)
  234. IpList = list;
  235. var ram = new Random().Next(0, list.Count);
  236. IpCount = list.Count;
  237. return list[ram];
  238. }
  239. static object locker = new object();
  240. static bool isGetIp = false;
  241. public delegate string GetIPDataBYOne(List<string> _urlList, string _title = "", bool isFormData = false);
  242. public delegate string GetIPDataBYOne_FormData(List<string> _urlList, Dictionary<string, string> formData, string _title = "");
  243. /// <summary>
  244. /// 获取HTML
  245. /// </summary>
  246. /// <param name="model">参数实体</param>
  247. /// <returns></returns>
  248. public static HtmlDocument GetHtmlHtmlDocument(HtmlParameterDTO model)
  249. {
  250. var html = GetHtmlByIP(model);
  251. var doc = new HtmlDocument();
  252. doc.LoadHtml(html);
  253. return doc;
  254. }
  255. /// <summary>
  256. /// 获取HTML
  257. /// </summary>
  258. /// <param name="model">参数实体</param>
  259. /// <returns></returns>
  260. public static string GetHtmlString(HtmlParameterDTO model)
  261. {
  262. return GetHtmlByIP(model);
  263. }
  264. /// <summary>
  265. /// 通过Ip获取页面的HTML
  266. /// </summary>
  267. /// <param name="model"></param>
  268. /// <returns></returns>
  269. private static string GetHtmlByIP(HtmlParameterDTO model)
  270. {
  271. var NotIpList = new List<string>();
  272. Stopwatch sw = new Stopwatch();
  273. sw.Start();
  274. var ip = model.IP.IsEmpty() ? GetIp() : model.IP;
  275. var httpItem = new HttpItem();
  276. lock (locker)
  277. {
  278. httpItem = Mapper<HttpItem>(model);
  279. httpItem.WebProxy = new WebProxy(ip);
  280. }
  281. var html = new HttpHelper().GetHtml(httpItem);
  282. //对文本的检查
  283. while ((model.IsCheckEmpty && html.Html.IsEmpty())
  284. || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString()
  285. || (html.Html.Contains("403") && html.Html.ToLower().Contains("forbidden"))
  286. || ((html.Html.Contains("HTTP Status 404") || html.Html.Contains("404 Not Found")) && html.Html.ToLower().Contains("not found"))
  287. || html.Html.Contains("502 Bad Gateway")
  288. || html.Html.Contains("400 Bad Request")
  289. || (html.Html.Contains("301 Moved Permanently") && html.Html.ToLower().Contains("moved permanently"))
  290. || (html.Html.Contains("The requested URL could not be retrieved") && html.Html.ToLower().Contains("could not be retrieved"))
  291. || html.Html.Contains("缓存访问被拒绝")
  292. || html.Html.Contains("无效用户")
  293. || (!model.Title.IsEmpty() && !html.Html.Contains(model.Title)))
  294. {
  295. if (html.Html.ToLower().Contains("exception report"))
  296. {
  297. return ConfigurationManager.AppSettings["Termination"].ToString();
  298. }
  299. NotIpList.Add(ip);
  300. if (NotIpList.Distinct().ToList().Count == IpCount || NotIpList.Distinct().ToList().Count > model.NotIpNumber)
  301. {
  302. //EmailHelper.Send("1625453870@qq.com", "IP用完,未获取值的URL", "URL:" + model.Url + "||参数:" + model.FormData.TryToJson());
  303. //return ConfigurationManager.AppSettings["Termination"].ToString();
  304. LogBD(model.Url, "LogUrl", "UrlLog");
  305. return ConfigurationManager.AppSettings["Termination"].ToString();
  306. //IAsyncResult asyncResult;
  307. //lock (locker)
  308. //{
  309. // GetIPDataBYOne task = new GetIPDataBYOne(IPHelper.GetIPDataBYOne);
  310. // asyncResult = task.BeginInvoke(new List<string> { model.Url }, model.Title, false, null, null);
  311. // while (asyncResult != null && !asyncResult.AsyncWaitHandle.WaitOne(100, false))
  312. // {
  313. // }
  314. // ip = task.EndInvoke(asyncResult);
  315. // if (ip.IsEmpty())
  316. // return ConfigurationManager.AppSettings["Termination"].ToString();
  317. // else
  318. // return ip;
  319. //}
  320. }
  321. else
  322. {
  323. ip = ip = model.IP.IsEmpty() ? GetIp() : model.IP;
  324. while (NotIpList.Contains(ip) && model.IP.IsEmpty())
  325. ip = ip = model.IP.IsEmpty() ? GetIp() : model.IP;
  326. }
  327. httpItem.WebProxy = new WebProxy(ip);
  328. html = new HttpHelper().GetHtml(httpItem);
  329. }
  330. sw.Stop();
  331. Trace.WriteLine("url:" + model.Url + "||IP:" + ip + "||时间:" + sw.ElapsedMilliseconds + "毫秒");
  332. return html.Html;
  333. }
  334. /// <summary>
  335. /// 得到HtmlDocument
  336. /// </summary>
  337. /// <param name="url"></param>
  338. /// <param name="method"></param>
  339. /// <returns></returns>
  340. public static HtmlDocument GetHtml(string url, string title = "", bool isWebSoxket = false, string webProxy = "", string method = "get", int timeout = 90 * 1000, int notIpNUmber = 100)
  341. {
  342. return GetHtmlHtmlDocument(new HtmlParameterDTO
  343. {
  344. Url = url,
  345. Title = title,
  346. IP = webProxy,
  347. Method = method,
  348. Timeout = timeout,
  349. NotIpNumber = notIpNUmber
  350. });
  351. }
  352. /// <summary>
  353. /// 得到HtmlDocument
  354. /// From表单提交
  355. /// </summary>
  356. /// <param name="url"></param>
  357. /// <param name="method"></param>
  358. /// <returns></returns>
  359. public static HtmlDocument GetHtml(string url, Dictionary<string, string> formData, string title = "", string webProxy = "", int timeout = 90 * 1000, int notIpNUmber = 100)
  360. {
  361. // ContentType = "application/x-www-form-urlencoded",
  362. return GetHtmlHtmlDocument(new HtmlParameterDTO
  363. {
  364. Url = url,
  365. Title = title,
  366. IP = webProxy,
  367. Method = "POST",
  368. Timeout = timeout,
  369. NotIpNumber = notIpNUmber,
  370. ContentType = "application/x-www-form-urlencoded",
  371. FormData = formData
  372. });
  373. }
  374. #endregion
  375. #region WebClient请求
  376. private static string GetHtmlByWebClient(HtmlParameterDTO model)
  377. {
  378. try
  379. {
  380. Thread.Sleep(1000);
  381. using (var webClient = new WebClient())
  382. {
  383. webClient.Credentials = CredentialCache.DefaultCredentials;
  384. byte[] pageDate = webClient.DownloadData(model.Url);
  385. var content = Encoding.UTF8.GetString(pageDate);
  386. webClient.Dispose();
  387. return content;
  388. }
  389. }
  390. catch (Exception)
  391. {
  392. return GetHtmlByWebClient(model);
  393. }
  394. }
  395. #endregion
  396. #region lg
  397. /// <summary>
  398. /// 得到HtmlDocument
  399. /// </summary>
  400. /// <param name="url"></param>
  401. /// <param name="method"></param>
  402. /// <returns></returns>
  403. public static string GetHtmlString(string url, string title = "", int timeout = 90 * 1000, bool isWebSoxket = false, string webProxy = "", string method = "get", int notIpNUmber = 100)
  404. {
  405. var NotIpList = new List<string>();
  406. var ip = webProxy.IsEmpty() ? GetIp() : webProxy;
  407. var html = new HttpHelper().GetHtml(new HttpItem
  408. {
  409. Url = url,
  410. Method = method,
  411. WebProxy = new WebProxy(ip),
  412. Timeout = timeout
  413. });
  414. int number = 0;
  415. if (!isWebSoxket)
  416. {
  417. while (html.Html.IsEmpty() || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString() || (html.Html.IndexOf("403") != -1 && html.Html.ToLower().IndexOf("forbidden") != -1)
  418. || (html.Html.IndexOf("HTTP Status 404") != -1 && html.Html.ToLower().IndexOf("not found") != -1)
  419. || (!title.IsEmpty() && html.Html.IndexOf(title) == -1)
  420. || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString()
  421. || (html.Html.Contains("403") && html.Html.ToLower().Contains("forbidden"))
  422. || ((html.Html.Contains("HTTP Status 404") || html.Html.Contains("404 Not Found")) && html.Html.ToLower().Contains("not found"))
  423. || html.Html.Contains("502 Bad Gateway")
  424. || html.Html.Contains("400 Bad Request")
  425. || (html.Html.Contains("301 Moved Permanently") && html.Html.ToLower().Contains("moved permanently"))
  426. || (html.Html.Contains("The requested URL could not be retrieved") && html.Html.ToLower().Contains("could not be retrieved"))
  427. || html.Html.Contains("缓存访问被拒绝")
  428. || html.Html.Contains("操作太频繁了,请先歇一歇")
  429. || (title.IsEmpty() && !html.Html.Contains(title)))
  430. {
  431. number++;
  432. if (number > 40)
  433. {
  434. if (html.Html.Contains("操作太频繁了,请先歇一歇"))
  435. {
  436. return html.Html;
  437. }
  438. else
  439. {
  440. return "";
  441. }
  442. }
  443. if (html.Html.ToLower().Contains("exception report"))
  444. {
  445. ConfigurationManager.AppSettings["Termination"].ToString();
  446. break;
  447. }
  448. NotIpList.Add(ip);
  449. if (NotIpList.Distinct().ToList().Count == IpCount)
  450. {
  451. //IAsyncResult asyncResult;
  452. //lock (locker)
  453. //{
  454. // GetIPDataBYOne task = new GetIPDataBYOne(IPHelper.GetIPDataBYOne);
  455. // asyncResult = task.BeginInvoke(new List<string> { url }, title, false, null, null);
  456. // while (asyncResult != null && !asyncResult.AsyncWaitHandle.WaitOne(100, false))
  457. // {
  458. // }
  459. // ip = task.EndInvoke(asyncResult);
  460. // if (ip.IsEmpty())
  461. // {
  462. return ConfigurationManager.AppSettings["Termination"].ToString();
  463. // break;
  464. // }
  465. //}
  466. //}
  467. }
  468. else
  469. ip = webProxy.IsEmpty() ? GetIp() : webProxy;
  470. while (NotIpList.Contains(ip))
  471. ip = webProxy.IsEmpty() ? GetIp() : webProxy;
  472. html = new HttpHelper().GetHtml(new HttpItem
  473. {
  474. Url = url,
  475. Method = method,
  476. WebProxy = new WebProxy(ip)
  477. });
  478. }
  479. }
  480. return html.Html;
  481. }
  482. /// <summary>
  483. /// 得到HtmlDocument
  484. /// </summary>
  485. /// <param name="url"></param>
  486. /// <param name="method"></param>
  487. /// <returns></returns>
  488. public static HttpResult GetPostHtmlString(string url, HttpItem model, string title = "", int timeout = 90 * 1000, bool isWebSoxket = false, string webProxy = "", string method = "get", int notIpNUmber = 100)
  489. {
  490. model.Timeout = timeout;
  491. var html = new HttpHelper().GetHtml(model);
  492. if (!isWebSoxket)
  493. {
  494. var number = 0;
  495. while (html.Html.IsEmpty() || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString() || (html.Html.IndexOf("403") != -1 && html.Html.ToLower().IndexOf("forbidden") != -1)
  496. || (html.Html.IndexOf("HTTP Status 404") != -1 && html.Html.ToLower().IndexOf("not found") != -1)
  497. || (!title.IsEmpty() && html.Html.IndexOf(title) == -1))
  498. {
  499. number++;
  500. if (number > 20)
  501. return html;
  502. model.WebProxy = new WebProxy(GetIp());
  503. html = new HttpHelper().GetHtml(model);
  504. }
  505. }
  506. return html;
  507. }
  508. /// <summary>
  509. /// 得到HtmlDocument
  510. /// </summary>
  511. /// <param name="url"></param>
  512. /// <param name="method"></param>
  513. /// <returns></returns>
  514. public static string GetHtmlString_jb(string url, string title = "", int timeout = 90 * 1000, bool isWebSoxket = false, string webProxy = "", string method = "get", int notIpNUmber = 100)
  515. {
  516. var NotIpList = new List<string>();
  517. var html = new HttpHelper().GetHtml(new HttpItem
  518. {
  519. Url = url,
  520. Method = method,
  521. Timeout = timeout
  522. });
  523. int number = 0;
  524. int number1 = 0;
  525. if (!isWebSoxket)
  526. {
  527. while (html.Html.IsEmpty() || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString() || (html.Html.IndexOf("403") != -1 && html.Html.ToLower().IndexOf("forbidden") != -1)
  528. || (html.Html.IndexOf("HTTP Status 404") != -1 && html.Html.ToLower().IndexOf("not found") != -1)
  529. || (!title.IsEmpty() && html.Html.IndexOf(title) == -1)
  530. || html.Html == ConfigurationManager.AppSettings["HttpException"].ToString()
  531. || (html.Html.Contains("403") && html.Html.ToLower().Contains("forbidden"))
  532. || ((html.Html.Contains("HTTP Status 404") || html.Html.Contains("404 Not Found")) && html.Html.ToLower().Contains("not found"))
  533. || html.Html.Contains("502 Bad Gateway")
  534. || html.Html.Contains("400 Bad Request")
  535. || (html.Html.Contains("301 Moved Permanently") && html.Html.ToLower().Contains("moved permanently"))
  536. || (html.Html.Contains("The requested URL could not be retrieved") && html.Html.ToLower().Contains("could not be retrieved"))
  537. || html.Html.Contains("缓存访问被拒绝")
  538. || html.Html.Contains("操作太频繁了,请先歇一歇")
  539. || (title.IsEmpty() && !html.Html.Contains(title)))
  540. {
  541. if (html.Html.Contains("操作太频繁了,请先歇一歇"))
  542. {
  543. number1++;
  544. Thread.Sleep(3000);
  545. if (number1 > 39)
  546. return html.Html;
  547. }
  548. number++;
  549. if (number > 40)
  550. return "";
  551. if (html.Html.ToLower().Contains("exception report"))
  552. {
  553. ConfigurationManager.AppSettings["Termination"].ToString();
  554. break;
  555. }
  556. html = new HttpHelper().GetHtml(new HttpItem
  557. {
  558. Url = url,
  559. Method = method,
  560. });
  561. }
  562. }
  563. return html.Html;
  564. }
  565. public static string GetHtmlString_ceshi(string url = "http://fenxi.zgzcw.com/2321966/bjop", string title = "", bool isWebSoxket = false, string webProxy = "", string method = "get")
  566. {
  567. var html3 = new HttpHelper().GetHtml(new HttpItem
  568. {
  569. Url = url,
  570. Method = method,
  571. WebProxy = new WebProxy("113.124.93.1:601")
  572. });
  573. html3 = new HttpHelper().GetHtml(new HttpItem
  574. {
  575. Url = url,
  576. Method = method,
  577. WebProxy = new WebProxy("117.91.249.95:601")
  578. });
  579. F_Grouping g = new F_Grouping();
  580. List<string> list = new List<string>();
  581. list.Add("27.26.162.129");
  582. list.Add("117.91.249.95");
  583. list.Add("222.189.190.117");
  584. list.Add("222.189.191.254");
  585. list.Add("111.72.57.234");
  586. list.Add("113.124.93.1");
  587. list.Add("60.189.167.102");
  588. list.Add("180.118.141.126");
  589. list.Add("221.230.123.45");
  590. list.Add("221.230.123.101");
  591. list.Add("221.230.124.127");
  592. list.Add("125.125.45.149");
  593. list.Add("182.34.32.90");
  594. list.Add("113.121.46.19");
  595. list.Add("113.121.45.103");
  596. list.Add("113.121.23.219");
  597. list.Add("111.72.56.135");
  598. list.Add("111.72.63.12");
  599. list.Add("111.72.62.174");
  600. list.Add("111.72.58.28");
  601. list.Add("111.72.62.226");
  602. list.Add("106.226.227.245");
  603. list.Add("111.79.173.163");
  604. list.Add("106.7.78.39");
  605. list.Add("182.84.86.158");
  606. list.Add("182.100.238.11");
  607. list.Add("111.72.107.203");
  608. List<int> ss = new List<int>();
  609. for (int i = 0; i < 65535; i++)
  610. {
  611. ss.Add(i + 1);
  612. }
  613. int max1 = list.Count;
  614. int num1 = 0;
  615. list.ForEach(async p =>
  616. {
  617. await Task.Run(() =>
  618. {
  619. int max = 65535;
  620. int num = 0;
  621. //比赛
  622. ss.ForEach(async p1 =>
  623. {
  624. await Task.Run(() =>
  625. {
  626. var html = new HttpHelper().GetHtml(new HttpItem
  627. {
  628. Url = url,
  629. Method = method,
  630. WebProxy = new WebProxy(p + ":" + p1)
  631. });
  632. });
  633. lock (g)
  634. {
  635. num++;
  636. Monitor.Pulse(g); //完成,通知等待队列,告知已完,执行下一个。
  637. }
  638. });
  639. lock (g)
  640. {
  641. while (num < max)
  642. {
  643. Monitor.Wait(g);//等待
  644. }
  645. }
  646. });
  647. lock (g)
  648. {
  649. num1++;
  650. Monitor.Pulse(g); //完成,通知等待队列,告知已完,执行下一个。
  651. }
  652. });
  653. lock (g)
  654. {
  655. while (num1 < max1)
  656. {
  657. Monitor.Wait(g);//等待
  658. }
  659. }
  660. //if (!isWebSoxket)
  661. //{
  662. // var number = 0;
  663. // while (html.Html.IsEmpty() || (html.Html.IndexOf("403") != -1 && html.Html.ToLower().IndexOf("forbidden") != -1)
  664. // || (html.Html.IndexOf("HTTP Status 404") != -1 && html.Html.ToLower().IndexOf("not found") != -1)
  665. // || (!title.IsEmpty() && html.Html.IndexOf(title) == -1))
  666. // {
  667. // number++;
  668. // if (number > 100)
  669. // return null;
  670. // //if (number > IpCount)
  671. // html = new HttpHelper().GetHtml(new HttpItem
  672. // {
  673. // Url = url,
  674. // Method = method,
  675. // WebProxy = new WebProxy(CommonHelper.GetIp())
  676. // });
  677. // }
  678. //}
  679. return "";
  680. }
  681. #endregion
  682. #region 时间
  683. /// <summary>
  684. /// 将c# DateTime时间格式转换为Unix时间戳格式
  685. /// </summary>
  686. /// <param name="time">时间</param>
  687. /// <returns>long</returns>
  688. public static long ConvertDateTimeToInt(System.DateTime time)
  689. {
  690. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
  691. long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
  692. return t;
  693. }
  694. /// <summary>
  695. /// 时间戳转时间
  696. /// </summary>
  697. /// <param name="unixTimeStamp"></param>
  698. /// <returns></returns>
  699. public static DateTime ConvertIntToDateTime(string unixTimeStamp)
  700. {
  701. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  702. long lTime = long.Parse(unixTimeStamp + "0000");
  703. TimeSpan toNow = new TimeSpan(lTime);
  704. DateTime targetDt = dtStart.Add(toNow);
  705. return dtStart.Add(toNow);
  706. }
  707. #endregion
  708. /// <summary>
  709. /// 线程是否执行完成
  710. /// </summary>
  711. /// <returns></returns>
  712. public static bool ThreadsFinsh()
  713. {
  714. int maxWorkerThreads, workerThreads;
  715. int maxportThreads, portThreads;
  716. /*
  717. GetAvailableThreads():检索由 GetMaxThreads 返回的线程池线程的最大数目和当前活动数目之间的差值。
  718. 而GetMaxThreads 检索可以同时处于活动状态的线程池请求的数目。
  719. 通过最大数目减可用数目就可以得到当前活动线程的数目,如果为零,那就说明没有活动线程,说明所有线程运行完毕。
  720. */
  721. ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxportThreads);
  722. ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
  723. Thread.Sleep(3000);
  724. Trace.WriteLine("正在执行任务的线程数" + (maxWorkerThreads - workerThreads));
  725. if (maxWorkerThreads - workerThreads == 0)
  726. {
  727. Trace.WriteLine("加载完成!");
  728. return true;
  729. }
  730. return false;
  731. }
  732. /// <summary>
  733. /// 线程是否执行完成
  734. /// </summary>
  735. /// <returns></returns>
  736. public static bool ThreadsFinsh_new()
  737. {
  738. int maxWorkerThreads, workerThreads;
  739. int maxportThreads, portThreads;
  740. /*
  741. GetAvailableThreads():检索由 GetMaxThreads 返回的线程池线程的最大数目和当前活动数目之间的差值。
  742. 而GetMaxThreads 检索可以同时处于活动状态的线程池请求的数目。
  743. 通过最大数目减可用数目就可以得到当前活动线程的数目,如果为零,那就说明没有活动线程,说明所有线程运行完毕。
  744. */
  745. ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxportThreads);
  746. ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
  747. Thread.Sleep(3000);
  748. Trace.WriteLine("正在执行任务的线程数" + (maxWorkerThreads - workerThreads - 3));
  749. if (maxWorkerThreads - workerThreads - 3 == 0)
  750. {
  751. Trace.WriteLine("加载完成!");
  752. return true;
  753. }
  754. return false;
  755. }
  756. /// <summary>
  757. /// 文件写入
  758. /// </summary>
  759. public static void Write(string path, string data)
  760. {
  761. using (var fs = new FileStream(path, FileMode.Append))
  762. {
  763. using (var sw = new StreamWriter(fs))
  764. {
  765. sw.WriteLine(data);
  766. sw.Flush();
  767. }
  768. }
  769. }
  770. public static void Write_IP(string path, string data)
  771. {
  772. using (var fs = new FileStream(path, FileMode.Create))
  773. {
  774. using (var sw = new StreamWriter(fs, Encoding.UTF8))
  775. {
  776. sw.Write(data);
  777. sw.Flush();
  778. }
  779. }
  780. }
  781. public static void LogBD(string content, string pathName = "", string directoryName = "Log")
  782. {
  783. if (pathName.IsEmpty())
  784. pathName = content;
  785. var path = AppDomain.CurrentDomain.BaseDirectory + "/" + directoryName;
  786. CreateDirectory(path);
  787. path += $"/{DateTime.Now.ToString("yyyyMMdd")}";
  788. CreateDirectory(path);
  789. Write(path + $"/{pathName}.txt", content + "||" + DateTime.Now.ToString());
  790. }
  791. /// <summary>
  792. /// 日志记录
  793. /// </summary>
  794. /// <param name="type"></param>
  795. /// <param name="action"></param>
  796. public static void LogBD(Type type, Action action)
  797. {
  798. try
  799. {
  800. action();
  801. }
  802. catch (Exception ex)
  803. {
  804. var nameArray = type.FullName.Split('.');
  805. LogBD(ex.Message, nameArray[nameArray.Length - 1], "Cw");
  806. LogBD($"{ MethodBase.GetCurrentMethod().DeclaringType.FullName}异常", "CW", "Cw");
  807. }
  808. }
  809. /// <summary>
  810. /// 日志记录
  811. /// </summary>
  812. /// <param name="type"></param>
  813. /// <param name="action"></param>
  814. public static T LogBD<T>(Type type, Func<T> action) where T : new()
  815. {
  816. try
  817. {
  818. return action();
  819. }
  820. catch (Exception ex)
  821. {
  822. var nameArray = type.FullName.Split('.');
  823. LogBD(ex.Message, nameArray[nameArray.Length - 1], "Cw");
  824. LogBD($"{ MethodBase.GetCurrentMethod().DeclaringType.FullName}异常", "CW", "Cw");
  825. return new T();
  826. }
  827. }
  828. /// <summary>
  829. /// 创建文件夹
  830. /// </summary>
  831. /// <param name="paht"></param>
  832. private static void CreateDirectory(string path)
  833. {
  834. if (!Directory.Exists(path))
  835. Directory.CreateDirectory(path);
  836. }
  837. public static T Mapper<T>(object data)
  838. {
  839. return AutoMapper.Mapper.DynamicMap<T>(data);
  840. }
  841. //获取外网IP
  842. public static string GetExternalIP()
  843. {
  844. using (var webClient = new WebClient())
  845. {
  846. try
  847. {
  848. webClient.Credentials = CredentialCache.DefaultCredentials;
  849. byte[] pageDate = webClient.DownloadData("http://pv.sohu.com/cityjson?ie=utf-8");
  850. String ip = Encoding.UTF8.GetString(pageDate); webClient.Dispose();
  851. Match rebool = Regex.Match(ip, @"\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); return rebool.Value;
  852. }
  853. catch (Exception e)
  854. {
  855. return "";
  856. }
  857. }
  858. }
  859. }
  860. class AsyncSemaphore
  861. {
  862. private readonly static Task s_completed = Task.FromResult(true);
  863. private readonly Queue<TaskCompletionSource<bool>> m_waiters = new Queue<TaskCompletionSource<bool>>();
  864. private int m_currentCount;
  865. public AsyncSemaphore(int initialCount)
  866. {
  867. if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");
  868. m_currentCount = initialCount;
  869. }
  870. public Task WaitAsync()
  871. {
  872. lock (m_waiters)
  873. {
  874. if (m_currentCount > 0)
  875. {
  876. --m_currentCount;
  877. return s_completed;
  878. }
  879. else
  880. {
  881. var waiter = new TaskCompletionSource<bool>();
  882. m_waiters.Enqueue(waiter);
  883. return waiter.Task;
  884. }
  885. }
  886. }
  887. public void Release()
  888. {
  889. TaskCompletionSource<bool> toRelease = null;
  890. lock (m_waiters)
  891. {
  892. if (m_waiters.Count > 0)
  893. toRelease = m_waiters.Dequeue();
  894. else
  895. ++m_currentCount;
  896. }
  897. if (toRelease != null)
  898. toRelease.SetResult(true);
  899. }
  900. }
  901. public class AsyncLock
  902. {
  903. private readonly AsyncSemaphore m_semaphore;
  904. private readonly Task<Releaser> m_releaser;
  905. public AsyncLock()
  906. {
  907. m_semaphore = new AsyncSemaphore(1);
  908. m_releaser = Task.FromResult(new Releaser(this));
  909. }
  910. public Task<Releaser> LockAsync()
  911. {
  912. var wait = m_semaphore.WaitAsync();
  913. return wait.IsCompleted ?
  914. m_releaser :
  915. wait.ContinueWith((_, state) => new Releaser((AsyncLock)state),
  916. this, CancellationToken.None,
  917. TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
  918. }
  919. public struct Releaser : IDisposable
  920. {
  921. private readonly AsyncLock m_toRelease;
  922. internal Releaser(AsyncLock toRelease) { m_toRelease = toRelease; }
  923. public void Dispose()
  924. {
  925. if (m_toRelease != null)
  926. m_toRelease.m_semaphore.Release();
  927. }
  928. }
  929. }
  930. }