123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using FCS.Common;
- using HtmlAgilityPack;
- using System;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace FCS.Crawler.Tools
- {
- public class CommonMethod
- {
- #region 获取全局唯一GUID
- /// <summary>
- /// 获取全局唯一GUID
- /// </summary>
- /// <param name="needReplace">是否需要替换-</param>
- /// <param name="format">格式化</param>
- /// <example>N:38bddf48f43c48588e0d78761eaa1ce6</example>>
- /// <example>P:(778406c2-efff-4262-ab03-70a77d09c2b5)</example>>
- /// <example>B:{09f140d5-af72-44ba-a763-c861304b46f8}</example>>
- /// <example>D:57d99d89-caab-482a-a0e9-a0a803eed3ba</example>>
- /// <returns></returns>
- public static string GetGuid(bool needReplace = true, string format = "N")
- {
- Guid res = NewSequentialGuid();//Guid.NewGuid();
- return needReplace ? res.ToString(format) : res.ToString();
- }
- [System.Runtime.InteropServices.DllImport("rpcrt4.dll", SetLastError = true)]
- private static extern int UuidCreateSequential(byte[] buffer);
- internal static object GetUrl(string Url)
- {
- string js = "";
- HtmlNode node = null;
- var htmlResource = NetHelper.GetUrlResponse(Url, Encoding.GetEncoding("utf-8"));
- js = GetJs(htmlResource);
- return js;
- }
- /// <summary>
- /// 创建有序GUID
- /// </summary>
- /// <returns></returns>
- private static Guid NewSequentialGuid()
- {
- byte[] raw = new byte[16];
- if (UuidCreateSequential(raw) != 0)
- throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
- byte[] fix = new byte[16];
- // 反转 0..3
- fix[0x0] = raw[0x3];
- fix[0x1] = raw[0x2];
- fix[0x2] = raw[0x1];
- fix[0x3] = raw[0x0];
- // 反转 4 & 5
- fix[0x4] = raw[0x5];
- fix[0x5] = raw[0x4];
- // 反转 6 & 7
- fix[0x6] = raw[0x7];
- fix[0x7] = raw[0x6];
- // 后8位不做操作,实际为当前MAC地址
- fix[0x8] = raw[0x8];
- fix[0x9] = raw[0x9];
- fix[0xA] = raw[0xA];
- fix[0xB] = raw[0xB];
- fix[0xC] = raw[0xC];
- fix[0xD] = raw[0xD];
- fix[0xE] = raw[0xE];
- fix[0xF] = raw[0xF];
- return new Guid(fix);
- }
- #endregion 获取全局唯一GUID
- #region 常用的html获取js
- /// <summary>
- /// 根据htmL获取Js
- /// </summary>
- /// <param name="Url"></param>
- /// <returns></returns>
- public static string GetUrl()
- {
- string js = "";
- HtmlNode node = null;
- var htmlResource = NetHelper.GetUrlResponse("", Encoding.GetEncoding("utf-8"));
- js = GetJs(htmlResource);
- return js;
- }
- public static string GetJs(string html)
- {
- string js1 = "";
- //正则表达式获取jsData后面的js
- Regex r1 = new Regex(@"jsData/(\w|\W)*?"">");
- MatchCollection m1 = r1.Matches(html);
- foreach (Match item in m1)
- {
- js1 = item.Value.Replace("\">", "");
- }
- if (string.IsNullOrEmpty((js1)))
- {
- return "";
- }
- else
- {
- return js1;
- }
- }
- #endregion 常用的html获取js
- #region 根据htmL获取Js
- public static string HttpJs(string url)
- {
- WebRequest req = WebRequest.Create(url);
- WebResponse resp = req.GetResponse();
- StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
- string html = reader.ReadToEnd();
- return html;
- }
- #endregion 根据htmL获取Js
- }
- }
|