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
///
/// 获取全局唯一GUID
///
/// 是否需要替换-
/// 格式化
/// N:38bddf48f43c48588e0d78761eaa1ce6>
/// P:(778406c2-efff-4262-ab03-70a77d09c2b5)>
/// B:{09f140d5-af72-44ba-a763-c861304b46f8}>
/// D:57d99d89-caab-482a-a0e9-a0a803eed3ba>
///
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;
}
///
/// 创建有序GUID
///
///
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
///
/// 根据htmL获取Js
///
///
///
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
}
}