CommonHelper.cs 37 KB

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