增加老版本 KJDWebService
This commit is contained in:
1 parent
58abbb5b4f
commit
f84c4c1949
115 files changed
+36598
-18
No files matched your search
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using NaiBa.Util.Extension;
|
||||
|
||||
namespace NaiBa.Util.Helper
|
||||
{
|
||||
public class HtmlHelper
|
||||
{
|
||||
public static string HtmlEncode(string html)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return WebUtility.HtmlEncode(html);
|
||||
}
|
||||
}
|
||||
|
||||
public static string HtmlDecode(string url)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return WebUtility.HtmlDecode(url);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unicode解码
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public static string UnicodeDecode(string html)
|
||||
{
|
||||
html = WebUtility.HtmlDecode(html);
|
||||
|
||||
Regex reUnicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);
|
||||
return reUnicode.Replace(html, m =>
|
||||
{
|
||||
short c;
|
||||
if (short.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c))
|
||||
{
|
||||
return "" + (char)c;
|
||||
}
|
||||
return m.Value;
|
||||
});
|
||||
}
|
||||
|
||||
#region Resove
|
||||
public static string Resove(string html, string prefix, string subfix = "")
|
||||
{
|
||||
int inl = html.IndexOf(prefix);
|
||||
if (inl == -1)
|
||||
{
|
||||
prefix = prefix.Replace("\"", "'");
|
||||
subfix = subfix.Replace("\"", "'");
|
||||
inl = html.IndexOf(prefix);
|
||||
}
|
||||
if (inl == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
inl += prefix.Length;
|
||||
if (!string.IsNullOrEmpty(subfix))
|
||||
{
|
||||
int inl2 = html.IndexOf(subfix, inl);
|
||||
if (inl2 == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string result = html.Substring(inl, inl2 - inl);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
string result = html.Substring(inl);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ResoveReverse
|
||||
public static string ResoveReverse(string html, string subfix, string prefix)
|
||||
{
|
||||
int inl = html.IndexOf(subfix);
|
||||
if (inl == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string subString = html.Substring(0, inl);
|
||||
int inl2 = subString.LastIndexOf(prefix);
|
||||
if (inl2 == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string s = subString.Substring(inl2 + prefix.Length, subString.Length - inl2 - prefix.Length);
|
||||
return s;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ResoveList
|
||||
public static List<string> ResoveList(string html, string prefix, string subfix)
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
int index = 0;
|
||||
do
|
||||
{
|
||||
index = html.IndexOf(prefix, index);
|
||||
if (index == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
index += prefix.Length;
|
||||
int indexEnd = html.IndexOf(subfix, index);
|
||||
if (indexEnd > 0)
|
||||
{
|
||||
string content = html.Substring(index, indexEnd - index);
|
||||
list.Add(content);
|
||||
}
|
||||
index = indexEnd;
|
||||
}
|
||||
while (index > -1);
|
||||
return list;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ResoveListByAT
|
||||
public static List<string> ResoveListByAT(string html)
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
|
||||
int index = html.IndexOf('@');
|
||||
while (index > 0)
|
||||
{
|
||||
int indexx = html.LastIndexOf("'", index); //向前一个单引号
|
||||
int indexy = html.IndexOf("'", index + 1); //向后一个单引号
|
||||
string s78 = html.Substring(indexx, indexy - indexx);
|
||||
s78 = s78.Replace("'", "");
|
||||
list.Add(s78);
|
||||
index = html.IndexOf('@', indexy);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 清除HTML代码
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
public static string ClearHtml(string html)
|
||||
{
|
||||
html = html.ParseToString();
|
||||
html = Regex.Replace(html, @"(<head[\w\W]*?</head>|<script[\w\W]*?</script>|<style[\w\W]*?</style>|<!--[\w\W]*?-->)", string.Empty);
|
||||
html = Regex.Replace(html, @"(<span[\w\W]*?</span>)", string.Empty);
|
||||
html = Regex.Replace(html, "(<[^>]*?>)", string.Empty);
|
||||
return html;
|
||||
}
|
||||
|
||||
public static string GetHtmlText(string html)
|
||||
{
|
||||
html = html.ParseToString();
|
||||
html = Regex.Replace(html, "<.*?>", string.Empty);
|
||||
return html;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除HTML代码A标签
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public static string ClearHtmlA(string html)
|
||||
{
|
||||
html = html.ParseToString();
|
||||
html = Regex.Replace(html, "(</?a[^>]*>)", "", RegexOptions.IgnoreCase);
|
||||
return html.Trim();
|
||||
}
|
||||
|
||||
public static string AddEscapeCharacter(string html)
|
||||
{
|
||||
html = html.ParseToString();
|
||||
html = html.Replace("\"", "\\\"");
|
||||
html = html.Replace("'", "\\'");
|
||||
return html;
|
||||
}
|
||||
|
||||
public static string TrimBOM(string html)
|
||||
{
|
||||
html = html.Trim(new char[] { '\uFEFF' });
|
||||
return html;
|
||||
}
|
||||
|
||||
public static string TrimJsonp(string json)
|
||||
{
|
||||
int start = json.IndexOf("{");
|
||||
int end = json.LastIndexOf("}");
|
||||
if (start > 0 && end > 0)
|
||||
{
|
||||
return json.Substring(start, end - start + 1);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NaiBa.Util.Extension;
|
||||
|
||||
namespace NaiBa.Util.Helper
|
||||
{
|
||||
public class JsonHelper
|
||||
{
|
||||
public static bool IsJson(string html)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
html = html.Trim();
|
||||
if ((html.StartsWith("{") && html.EndsWith("}")) || // For object
|
||||
(html.StartsWith("[") && html.EndsWith("]"))) // For array
|
||||
{
|
||||
try
|
||||
{
|
||||
var obj = JToken.Parse(html);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static string JsonAlphabeticaly(string json)
|
||||
{
|
||||
json = json.ParseToString().Trim();
|
||||
if (json.StartsWith("{"))
|
||||
{
|
||||
var jObj = JObject.Parse(json);
|
||||
JObject result = new JObject();
|
||||
OrderJson(result, jObj);
|
||||
return result.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
var jArr = JArray.Parse(json);
|
||||
JArray result = new JArray();
|
||||
foreach (var arr in jArr)
|
||||
{
|
||||
result.Add(new JObject());
|
||||
OrderJson((JObject)result[result.Count - 1], (JObject)arr);
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static string SerializeAlphabeticaly(object obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new OrderedContractResolver() });
|
||||
}
|
||||
|
||||
public static string TrimJsonp(string json)
|
||||
{
|
||||
return HtmlHelper.TrimJsonp(json);
|
||||
}
|
||||
|
||||
#region 私有方法
|
||||
private static void OrderJson(JObject jObj, JObject item)
|
||||
{
|
||||
foreach (var property in item.Children<JProperty>().OrderBy(p => p.Name))
|
||||
{
|
||||
switch (property.Value.Type.ToString())
|
||||
{
|
||||
case "Array":
|
||||
var jArr = new JArray();
|
||||
jObj.Add(property.Name, jArr);
|
||||
for (int i = 0; i < property.Value.Count(); i++)
|
||||
{
|
||||
switch (property.Value[i].Type.ToString())
|
||||
{
|
||||
case "Object":
|
||||
var obj = new JObject();
|
||||
jArr.Add(obj);
|
||||
OrderJson(obj, (JObject)property.Value[i]);
|
||||
break;
|
||||
|
||||
default:
|
||||
jArr.Add(property.Value[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "Object":
|
||||
jObj.Add(property.Name, new JObject());
|
||||
OrderJson((JObject)jObj[property.Name], (JObject)property.Value);
|
||||
break;
|
||||
|
||||
default:
|
||||
jObj.Add(property.Name, property.Value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class OrderedContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
return base.CreateProperties(type, memberSerialization)
|
||||
.OrderBy(p => p.Order ?? int.MaxValue) // Honour any explit ordering first
|
||||
.ThenBy(p => p.PropertyName)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Linq.Expressions;
|
||||
using NaiBa.Util.Extension;
|
||||
|
||||
namespace NaiBa.Util.Helper
|
||||
{
|
||||
public class ReflectionHelper
|
||||
{
|
||||
private static ConcurrentDictionary<string, object> dictCache = new ConcurrentDictionary<string, object>();
|
||||
|
||||
#region 得到类里面的属性集合
|
||||
public static PropertyInfo[] GetProperties(Type type, string[] columns = null)
|
||||
{
|
||||
PropertyInfo[] properties = null;
|
||||
if (dictCache.ContainsKey(type.FullName))
|
||||
{
|
||||
properties = dictCache[type.FullName] as PropertyInfo[];
|
||||
}
|
||||
else
|
||||
{
|
||||
properties = type.GetProperties();
|
||||
dictCache.TryAdd(type.FullName, properties);
|
||||
}
|
||||
|
||||
if (columns != null && columns.Length > 0)
|
||||
{
|
||||
// 按columns顺序返回属性
|
||||
var columnPropertyList = new List<PropertyInfo>();
|
||||
foreach (var column in columns)
|
||||
{
|
||||
var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault();
|
||||
if (columnProperty != null)
|
||||
{
|
||||
columnPropertyList.Add(columnProperty);
|
||||
}
|
||||
}
|
||||
return columnPropertyList.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static List<MemberInfo> GetMembers(Type type)
|
||||
{
|
||||
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var members = new List<MemberInfo>();
|
||||
members.AddRange(type.GetProperties(flags));
|
||||
members.AddRange(type.GetFields(flags));
|
||||
return members;
|
||||
}
|
||||
|
||||
public static void SetObjectPropertyValue<T>(string propertyName, object value)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var field = type.GetProperty(propertyName);
|
||||
if (field != null)
|
||||
{
|
||||
field.SetValue(null, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetObjectPropertyValue(object obj, string propertyName, object value)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
PropertyInfo propertyInfo = GetProperties(obj.GetType()).Where(p => p.Name == propertyName).FirstOrDefault();
|
||||
// make sure object has the property we are after
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
switch (propertyInfo.PropertyType.FullName)
|
||||
{
|
||||
case "System.Int32": propertyInfo.SetValue(obj, value.ParseToInt(), null); break;
|
||||
case "System.Int64": propertyInfo.SetValue(obj, value.ParseToLong(), null); break;
|
||||
case "System.Decimal": propertyInfo.SetValue(obj, value.ParseToDecimal(), null); break;
|
||||
case "System.Double": propertyInfo.SetValue(obj, value.ParseToDouble(), null); break;
|
||||
default:
|
||||
if (propertyInfo.PropertyType.GenericTypeArguments.Length > 0)
|
||||
{
|
||||
switch (propertyInfo.PropertyType.GenericTypeArguments[0].FullName)
|
||||
{
|
||||
case "System.DateTime":
|
||||
if (!string.IsNullOrEmpty(value.ParseToString()))
|
||||
{
|
||||
propertyInfo.SetValue(obj, value.ParseToString().ParseToDateTime(), null);
|
||||
}
|
||||
break;
|
||||
|
||||
case "System.Decimal":
|
||||
propertyInfo.SetValue(obj, value.ParseToDecimal(), null);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
propertyInfo.SetValue(obj, value, null);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static object GetObjectPropertyValue(object obj, string propertyName)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
PropertyInfo propertyInfo = GetProperties(obj.GetType()).Where(p => p.Name == propertyName).FirstOrDefault();
|
||||
// make sure object has the property we are after
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
return propertyInfo.GetValue(obj);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetGenericName(Type type)
|
||||
{
|
||||
if (type.GetGenericArguments().Length > 0)
|
||||
{
|
||||
return GetGenericName(type.GetGenericArguments().FirstOrDefault());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type.GetGenericArguments().FirstOrDefault() != null)
|
||||
{
|
||||
return type.GetGenericArguments().FirstOrDefault().Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return type.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
|
||||
{
|
||||
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
|
||||
return expressionBody.Member.Name;
|
||||
}
|
||||
|
||||
public static void SetObjectValue<T>(ref T obj, MemberInfo member, object value)
|
||||
{
|
||||
// Boxing method used for modifying structures
|
||||
var boxed = obj.GetType().IsValueType ? (object)obj : obj;
|
||||
SetMemberValue(ref boxed, member, value);
|
||||
obj = (T)boxed;
|
||||
}
|
||||
|
||||
public static void SetMemberValue<T>(ref T obj, MemberInfo member, object value)
|
||||
{
|
||||
if (IsProperty(member))
|
||||
{
|
||||
var prop = (PropertyInfo)member;
|
||||
if (prop.SetMethod != null)
|
||||
{
|
||||
prop.SetValue(obj, value);
|
||||
}
|
||||
}
|
||||
else if (IsField(member))
|
||||
{
|
||||
var field = (FieldInfo)member;
|
||||
field.SetValue(obj, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static object GetMemberValue(object obj, MemberInfo member)
|
||||
{
|
||||
object result = null;
|
||||
if (IsProperty(member))
|
||||
{
|
||||
var prop = (PropertyInfo)member;
|
||||
result = prop.GetValue(obj, prop.GetIndexParameters().Count() == 1 ? new object[] { null } : null);
|
||||
}
|
||||
else if (IsField(member))
|
||||
{
|
||||
var field = (FieldInfo)member;
|
||||
result = field.GetValue(obj);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool CanWrite(MemberInfo member)
|
||||
{
|
||||
return IsProperty(member) ? ((PropertyInfo)member).CanWrite : IsField(member);
|
||||
}
|
||||
|
||||
public static bool CanRead(MemberInfo member)
|
||||
{
|
||||
return IsProperty(member) ? ((PropertyInfo)member).CanRead : IsField(member);
|
||||
}
|
||||
|
||||
#region 私有方法
|
||||
private static bool IsProperty(MemberInfo member)
|
||||
{
|
||||
return IsType(member.GetType(), typeof(PropertyInfo));
|
||||
}
|
||||
|
||||
private static bool IsField(MemberInfo member)
|
||||
{
|
||||
return IsType(member.GetType(), typeof(FieldInfo));
|
||||
}
|
||||
|
||||
private static bool IsType(System.Type type, System.Type targetType)
|
||||
{
|
||||
return type.Equals(targetType) || type.IsSubclassOf(targetType);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NaiBa.Util.Helper
|
||||
{
|
||||
public class RegexHelper
|
||||
{
|
||||
// <span\\sclass=\"rantings-num\".*?>(.+?)</span>
|
||||
|
||||
/// <summary>
|
||||
/// 返回正则对象所有结果
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <param name="regex"></param>
|
||||
/// <returns></returns>
|
||||
public static MatchCollection GetMatchCollection(string html, string regex)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return Regex.Matches(html, regex, RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回单个正则分析结果
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <param name="regex"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetMatch(string html, string regex, int index = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
MatchCollection mc = Regex.Matches(html, regex, RegexOptions.IgnoreCase);
|
||||
if (mc.Count > 0)
|
||||
{
|
||||
return mc[index].Groups[1].Value.ToString();
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static string GetMatchWithSingleLine(string html, string regex)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
MatchCollection mc = Regex.Matches(html, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
||||
if (mc.Count > 0)
|
||||
{
|
||||
return mc[0].Groups[1].ToString();
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static bool IsNumber(string str)
|
||||
{
|
||||
Regex regex = new Regex(@"^[0-9\.]+$");
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
|
||||
public static bool IsAlphabet(string str)
|
||||
{
|
||||
Regex regex = new Regex(@"^[A-Za-z]+$");
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
|
||||
public static bool IsAlphabetOrNumber(string str)
|
||||
{
|
||||
Regex regex = new Regex(@"^[A-Za-z0-9]+$");
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user