Utils.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Mail;
  9. using System.Reflection;
  10. using System.Runtime.InteropServices;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Web;
  15. using Microsoft.VisualBasic;
  16. namespace CB.Common
  17. {
  18. public class Utils
  19. {
  20. /// <summary>
  21. /// 记录日志
  22. /// </summary>
  23. /// <param name="msg"></param>
  24. public static void WriteLog(string msg)
  25. {
  26. string path = AppDomain.CurrentDomain.BaseDirectory + "\\Log.txt";
  27. using (StreamWriter sw = new StreamWriter(path, true))
  28. {
  29. sw.WriteLine(System.DateTime.Now.ToString() + " " + msg);
  30. sw.Close();
  31. }
  32. }
  33. #region Post With Pic
  34. /// <summary>
  35. /// HTTP POST方式请求数据(带图片)
  36. /// </summary>
  37. /// <param name="url">URL</param>
  38. /// <param name="param">POST的数据</param>
  39. /// <param name="fileByte">图片</param>
  40. /// <returns></returns>
  41. public static string HttpPost(string url, Dictionary<string, object> param, byte[] fileByte)
  42. {
  43. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  44. byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  45. HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  46. wr.ContentType = "multipart/form-data; boundary=" + boundary;
  47. wr.Method = "POST";
  48. wr.KeepAlive = true;
  49. wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  50. Stream rs = wr.GetRequestStream();
  51. string responseStr = null;
  52. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  53. foreach (string key in param.Keys)
  54. {
  55. rs.Write(boundarybytes, 0, boundarybytes.Length);
  56. string formitem = string.Format(formdataTemplate, key, param[key]);
  57. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  58. rs.Write(formitembytes, 0, formitembytes.Length);
  59. }
  60. rs.Write(boundarybytes, 0, boundarybytes.Length);
  61. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  62. string header = string.Format(headerTemplate, "pic", fileByte, "text/plain");
  63. byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  64. rs.Write(headerbytes, 0, headerbytes.Length);
  65. rs.Write(fileByte, 0, fileByte.Length);
  66. byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  67. rs.Write(trailer, 0, trailer.Length);
  68. rs.Close();
  69. WebResponse wresp = null;
  70. try
  71. {
  72. wresp = wr.GetResponse();
  73. Stream stream2 = wresp.GetResponseStream();
  74. StreamReader reader2 = new StreamReader(stream2);
  75. responseStr = reader2.ReadToEnd();
  76. }
  77. catch
  78. {
  79. throw;
  80. }
  81. finally
  82. {
  83. if (wresp != null)
  84. {
  85. wresp.Close();
  86. wresp = null;
  87. }
  88. wr = null;
  89. }
  90. return responseStr;
  91. }
  92. /// <summary>
  93. /// http POST请求url
  94. /// </summary>
  95. /// <param name="url">请求地址</param>
  96. /// <param name="postData">POST数据</param>
  97. /// <returns></returns>
  98. public static string GetHttpWebResponse(string url, string postData)
  99. {
  100. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  101. request.Method = "POST";
  102. request.ContentType = "application/x-www-form-urlencoded";
  103. request.ContentLength = postData.Length;
  104. request.Timeout = 10000;
  105. HttpWebResponse response = null;
  106. try
  107. {
  108. StreamWriter swRequestWriter = new StreamWriter(request.GetRequestStream());
  109. swRequestWriter.Write(postData);
  110. if (swRequestWriter != null)
  111. swRequestWriter.Close();
  112. response = (HttpWebResponse)request.GetResponse();
  113. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  114. {
  115. return reader.ReadToEnd();
  116. }
  117. }
  118. catch { return ""; }
  119. finally
  120. {
  121. if (response != null)
  122. response.Close();
  123. }
  124. }
  125. #endregion
  126. /// <summary>
  127. /// AppSetting 配置节点值
  128. /// </summary>
  129. /// <param name="key"></param>
  130. /// <returns></returns>
  131. public static string GetAppSeting(string key)
  132. {
  133. try
  134. { return new System.Configuration.AppSettingsReader().GetValue(key, typeof(string)).ToString(); }
  135. catch
  136. { return string.Empty; }
  137. }
  138. /// <summary>
  139. /// 返回字串长度
  140. /// 汉字=2
  141. /// </summary>
  142. /// <param name="str"></param>
  143. /// <returns></returns>
  144. public static int GetStringLength(string str)
  145. {
  146. return Encoding.Default.GetBytes(str).Length;
  147. }
  148. /// <summary>
  149. /// 取指定长度的字符串
  150. /// </summary>
  151. /// <param name="p_SrcString">要检查的字符串</param>
  152. /// <param name="p_StartIndex">起始位置</param>
  153. /// <param name="p_Length">指定长度</param>
  154. /// <param name="p_TailString">用于替换的字符串</param>
  155. /// <returns>截取后的字符串</returns>
  156. public static string GetSubString(string SrcString, int maxLength, string replaceStr = "")
  157. {
  158. string myResult = SrcString;
  159. if (0 >= maxLength)
  160. return myResult;
  161. byte[] bsSrcString = Encoding.Default.GetBytes(myResult);
  162. if (maxLength >= bsSrcString.Length)
  163. return myResult;
  164. int p_EndIndex = maxLength;
  165. int nRealLength = p_EndIndex;
  166. int[] anResultFlag = new int[p_EndIndex];
  167. byte[] bsResult = null;
  168. int nFlag = 0;
  169. for (int i = 0; i < p_EndIndex; i++)
  170. {
  171. if (bsSrcString[i] > 127)
  172. {
  173. nFlag++;
  174. if (nFlag == 3)
  175. nFlag = 1;
  176. }
  177. else
  178. nFlag = 0;
  179. anResultFlag[i] = nFlag;
  180. }
  181. if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_EndIndex - 1] == 1))
  182. nRealLength = p_EndIndex + 1;
  183. bsResult = new byte[nRealLength];
  184. Array.Copy(bsSrcString, 0, bsResult, 0, nRealLength);
  185. myResult = Encoding.Default.GetString(bsResult);
  186. return myResult + replaceStr;
  187. }
  188. /// <summary>
  189. /// 网站域名
  190. /// </summary>
  191. /// <returns></returns>
  192. public static string GetHostDomain()
  193. {
  194. return GetUrlDomainName(HttpContext.Current.Request.Url.AbsoluteUri);
  195. }
  196. /// <summary>
  197. /// 返回url的根域名
  198. /// </summary>
  199. /// <param name="url"></param>
  200. /// <returns></returns>
  201. public static string GetUrlDomainName(string url)
  202. {
  203. string p = @"^https?://(?<domain>[^/]+)(/|$)";
  204. Regex reg = new Regex(p, RegexOptions.IgnoreCase);
  205. Match m = reg.Match(url);
  206. return m.Groups["domain"].Value.ToString().Trim();
  207. }
  208. /// <summary>
  209. /// 返回URL中结尾的文件名
  210. /// </summary>
  211. /// <param name="url"></param>
  212. /// <returns></returns>
  213. public static string GetUrlFileName(string url)
  214. {
  215. if (string.IsNullOrEmpty(url))
  216. return "";
  217. string[] str = url.Split('/');
  218. return str[str.Length - 1].Split('?')[0];
  219. }
  220. /// <summary>
  221. /// 根据字符长度,返回n个*号
  222. /// </summary>
  223. /// <param name="str"></param>
  224. /// <returns></returns>
  225. public static string GetStar(string str)
  226. {
  227. int len = str.Length;
  228. StringBuilder sb = new StringBuilder();
  229. for (int i = 0; i < len; i++)
  230. {
  231. sb.Append("*");
  232. }
  233. return sb.ToString();
  234. }
  235. /// <summary>
  236. /// 将IP地址转为整数形式
  237. /// </summary>
  238. /// <returns>整数</returns>
  239. public static long IP2Long(IPAddress ip)
  240. {
  241. int x = 3;
  242. long o = 0;
  243. foreach (byte f in ip.GetAddressBytes())
  244. {
  245. o += (long)f << 8 * x--;
  246. }
  247. return o;
  248. }
  249. /// <summary>
  250. /// 将整数转为IP地址
  251. /// </summary>
  252. /// <returns>IP地址</returns>
  253. public static IPAddress Long2IP(long l)
  254. {
  255. byte[] b = new byte[4];
  256. for (int i = 0; i < 4; i++)
  257. {
  258. b[3 - i] = (byte)(l >> 8 * i & 255);
  259. }
  260. return new IPAddress(b);
  261. }
  262. /// <summary>
  263. /// 生成4位数字随机数
  264. /// </summary>
  265. /// <returns></returns>
  266. public static string GetRandomInt()
  267. {
  268. Random random = new Random();
  269. return (random.Next(1000, 9999).ToString());
  270. }
  271. /// <summary>
  272. /// MD5函数
  273. /// 返回32位大写结果.
  274. /// </summary>
  275. /// <param name="str">原始字符串</param>
  276. /// <returns>MD5结果</returns>
  277. public static string MD5(string str)
  278. {
  279. byte[] b = Encoding.UTF8.GetBytes(str);
  280. b = new MD5CryptoServiceProvider().ComputeHash(b);
  281. string ret = "";
  282. for (int i = 0; i < b.Length; i++)
  283. ret += b[i].ToString("x").PadLeft(2, '0');
  284. return ret.ToUpper();
  285. }
  286. /// <summary>
  287. /// 检测是否符合email格式
  288. /// </summary>
  289. /// <param name="strEmail">要判断的email字符串</param>
  290. /// <returns>判断结果</returns>
  291. public static bool IsValidEmail(string strEmail)
  292. {
  293. return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
  294. }
  295. /// <summary>
  296. /// 是否为ip
  297. /// </summary>
  298. /// <param name="ip"></param>
  299. /// <returns></returns>
  300. public static bool IsIP(string ip)
  301. {
  302. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  303. }
  304. /// <summary>
  305. /// 获得当前页面客户端的IP
  306. /// </summary>
  307. /// <returns>当前页面客户端的IP</returns>
  308. public static string GetRealIP()
  309. {
  310. string result = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];//网宿CDN 获取用户IP
  311. if (string.IsNullOrEmpty(result))
  312. {
  313. result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  314. }
  315. if (string.IsNullOrEmpty(result))
  316. { result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; }
  317. if (string.IsNullOrEmpty(result))
  318. { result = HttpContext.Current.Request.UserHostAddress; }
  319. if (string.IsNullOrEmpty(result) || !IsIP(result))
  320. { return "127.0.0.1"; }
  321. return result;
  322. }
  323. /// <summary>
  324. /// 写cookie值
  325. /// </summary>
  326. /// <param name="strName">名称</param>
  327. /// <param name="strValue">值</param>
  328. /// <param name="strValue">过期时间(分钟)</param>
  329. public static void WriteCookie(string strName, string strValue, int expires = 480)
  330. {
  331. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
  332. if (cookie == null)
  333. {
  334. cookie = new HttpCookie(strName);
  335. }
  336. cookie.Value = HttpContext.Current.Server.UrlEncode(strValue);
  337. cookie.Expires = DateTime.Now.AddMinutes(expires);
  338. HttpContext.Current.Response.AppendCookie(cookie);
  339. }
  340. /// <summary>
  341. /// 写Cookie值
  342. /// </summary>
  343. /// <param name="strName">名称</param>
  344. /// <param name="key">键</param>
  345. /// <param name="strValue">值</param>
  346. /// <param name="expires">过期时间</param>
  347. public static void WriteCookie(string strName, string key, string strValue, int expires = 480)
  348. {
  349. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
  350. if (cookie == null)
  351. {
  352. cookie = new HttpCookie(strName);
  353. }
  354. cookie[key] = HttpContext.Current.Server.UrlEncode(strValue);
  355. cookie.Expires = DateTime.Now.AddMinutes(expires);
  356. HttpContext.Current.Response.AppendCookie(cookie);
  357. }
  358. /// <summary>
  359. /// 读cookie值
  360. /// </summary>
  361. /// <param name="strName">名称</param>
  362. /// <returns>cookie值</returns>
  363. public static string GetCookie(string strName)
  364. {
  365. if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
  366. return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
  367. return "";
  368. }
  369. /// <summary>
  370. /// 读cookie值
  371. /// </summary>
  372. /// <param name="strName">名称</param>
  373. /// <returns>cookie值</returns>
  374. public static string GetCookie(string strName, string key)
  375. {
  376. if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
  377. return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
  378. return "";
  379. }
  380. /// <summary>
  381. /// 另外一个清理所有Cookie的方法
  382. /// </summary>
  383. public static void FlushAllCookie()
  384. {
  385. for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
  386. {
  387. HttpContext.Current.Response.Cookies[HttpContext.Current.Request.Cookies.Keys[i]].Expires = DateTime.Now.AddDays(-600);
  388. HttpContext.Current.Response.Cookies[HttpContext.Current.Request.Cookies.Keys[i]].Values.Clear();
  389. }
  390. }
  391. /// <summary>
  392. /// 移除Cookies
  393. /// </summary>
  394. /// <param name="cookiesName"></param>
  395. public static void RemoveCookies(string cookiesName)
  396. {
  397. if (string.IsNullOrEmpty(cookiesName))
  398. return;
  399. HttpCookie cookies = HttpContext.Current.Request.Cookies[cookiesName];
  400. if (null != cookies)
  401. {
  402. HttpContext.Current.Response.Cookies[cookiesName].Expires = DateTime.Now.AddDays(-600);
  403. HttpContext.Current.Response.Cookies[cookiesName].Values.Clear();
  404. }
  405. }
  406. /// <summary>
  407. /// 将全角数字转换为数字
  408. /// </summary>
  409. /// <param name="SBCCase"></param>
  410. /// <returns></returns>
  411. public static string SBCCaseToNumberic(string SBCCase)
  412. {
  413. char[] c = SBCCase.ToCharArray();
  414. for (int i = 0; i < c.Length; i++)
  415. {
  416. byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
  417. if (b.Length == 2)
  418. {
  419. if (b[1] == 255)
  420. {
  421. b[0] = (byte)(b[0] + 32);
  422. b[1] = 0;
  423. c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
  424. }
  425. }
  426. }
  427. return new string(c);
  428. }
  429. /// <summary>
  430. /// 移除转义字符 如&nbsp;
  431. /// </summary>
  432. /// <param name="content"></param>
  433. /// <returns></returns>
  434. public static string RemoveEscapeCharacter(string content)
  435. {
  436. if (string.IsNullOrEmpty(content))
  437. return "";
  438. return Regex.Replace(content, @"&[a-zA-Z]+?;", "", RegexOptions.IgnoreCase);
  439. }
  440. /// <summary>
  441. /// 移除Html标记
  442. /// </summary>
  443. /// <param name="content"></param>
  444. /// <returns></returns>
  445. public static string RemoveHtml(string content)
  446. {
  447. if (string.IsNullOrEmpty(content))
  448. return "";
  449. return Regex.Replace(content, @"<[^>]*>", "", RegexOptions.IgnoreCase);
  450. }
  451. /// <summary>
  452. /// 移除空白 换行 TAB 中文空白
  453. /// </summary>
  454. /// <param name="content"></param>
  455. /// <returns></returns>
  456. public static string RemoveSpacing(string content)
  457. {
  458. if (string.IsNullOrEmpty(content))
  459. return "";
  460. return Regex.Replace(content, @"\s", "", RegexOptions.IgnoreCase);
  461. }
  462. /// <summary>
  463. /// 检测是否有Sql危险字符
  464. /// </summary>
  465. /// <param name="str">要判断字符串</param>
  466. /// <returns>判断结果</returns>
  467. public static bool IsSafeSqlString(string str)
  468. {
  469. return Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
  470. }
  471. /// <summary>
  472. /// 检测字符号是否只包括数字、字母和中文
  473. /// </summary>
  474. /// <param name="str">要判断字符串</param>
  475. /// <returns>判断结果</returns>
  476. public static bool IsSafeUserInfoString(string str)
  477. {
  478. return Regex.IsMatch(str, "[\\s\\p{P}\n\r=<>$>+¥^]");
  479. }
  480. /// <summary>
  481. /// 判断是否为base64字符串
  482. /// </summary>
  483. /// <param name="str"></param>
  484. /// <returns></returns>
  485. public static bool IsBase64String(string str)
  486. {
  487. //A-Z, a-z, 0-9, +, /, =
  488. return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
  489. }
  490. /// <summary>
  491. /// 判断对象是否为Int32类型的数字
  492. /// </summary>
  493. /// <param name="Expression"></param>
  494. /// <returns></returns>
  495. public static bool IsNumeric(object Expression)
  496. {
  497. return Validator.IsNumeric(Expression);
  498. }
  499. /// <summary>
  500. /// 从HTML中获取文本,保留br,p,img
  501. /// </summary>
  502. /// <param name="HTML"></param>
  503. /// <returns></returns>
  504. public static string GetTextFromHTML(string HTML)
  505. {
  506. System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"</?(?!br|/?p|img)[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  507. return regEx.Replace(HTML, "");
  508. }
  509. /// <summary>
  510. /// 从HTML中获取文本,保留tr,td,strong,br,span,p
  511. /// </summary>
  512. /// <param name="HTML"></param>
  513. /// <returns></returns>
  514. public static string GetValueFromHTML(string HTML)
  515. {
  516. System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(@"<(table|/table|tbody|/tbody)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  517. return regEx.Replace(HTML, "");
  518. }
  519. public static bool IsDouble(object Expression)
  520. {
  521. return Validator.IsDouble(Expression);
  522. }
  523. /// <summary>
  524. /// object型转换为bool型
  525. /// </summary>
  526. /// <param name="strValue">要转换的字符串</param>
  527. /// <param name="defValue">缺省值</param>
  528. /// <returns>转换后的bool类型结果</returns>
  529. public static bool ObjectToBool(object expression, bool defValue)
  530. {
  531. return TypeConverter.ObjectToBool(expression, defValue);
  532. }
  533. /// <summary>
  534. /// string型转换为bool型
  535. /// </summary>
  536. /// <param name="strValue">要转换的字符串</param>
  537. /// <param name="defValue">缺省值</param>
  538. /// <returns>转换后的bool类型结果</returns>
  539. public static bool StrToBool(string expression, bool defValue)
  540. {
  541. return TypeConverter.StrToBool(expression, defValue);
  542. }
  543. /// <summary>
  544. /// 将对象转换为Int32类型
  545. /// </summary>
  546. /// <param name="expression">要转换的字符串</param>
  547. /// <param name="defValue">缺省值</param>
  548. /// <returns>转换后的int类型结果</returns>
  549. public static int StrToInt(object expression, int defValue)
  550. {
  551. return TypeConverter.ObjectToInt(expression, defValue);
  552. }
  553. /// <summary>
  554. /// 将字符串转换为Int32类型
  555. /// </summary>
  556. /// <param name="expression">要转换的字符串</param>
  557. /// <param name="defValue">缺省值</param>
  558. /// <returns>转换后的int类型结果</returns>
  559. public static int StrToInt(string expression, int defValue)
  560. {
  561. return TypeConverter.StrToInt(expression, defValue);
  562. }
  563. /// <summary>
  564. /// 将字符串转换为DateTime类型
  565. /// </summary>
  566. /// <param name="expression">要转换的字符串</param>
  567. /// <param name="defValue">缺省值</param>
  568. /// <returns>转换后的DateTime类型结果</returns>
  569. public static DateTime StrToDateTime(string expression, DateTime defValue)
  570. {
  571. return TypeConverter.StrToDateTime(expression, defValue);
  572. }
  573. /// <summary>
  574. /// Object型转换为float型
  575. /// </summary>
  576. /// <param name="strValue">要转换的字符串</param>
  577. /// <param name="defValue">缺省值</param>
  578. /// <returns>转换后的int类型结果</returns>
  579. public static float StrToFloat(object strValue, float defValue)
  580. {
  581. return TypeConverter.StrToFloat(strValue, defValue);
  582. }
  583. /// <summary>
  584. /// string型转换为float型
  585. /// </summary>
  586. /// <param name="strValue">要转换的字符串</param>
  587. /// <param name="defValue">缺省值</param>
  588. /// <returns>转换后的int类型结果</returns>
  589. public static float StrToFloat(string strValue, float defValue)
  590. {
  591. return TypeConverter.StrToFloat(strValue, defValue);
  592. }
  593. /// <summary>
  594. /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
  595. /// </summary>
  596. /// <param name="strNumber">要确认的字符串数组</param>
  597. /// <returns>是则返加true 不是则返回 false</returns>
  598. public static bool IsNumericArray(string[] strNumber)
  599. {
  600. return Validator.IsNumericArray(strNumber);
  601. }
  602. /// <summary>
  603. /// 得到距离开奖的时间差
  604. /// </summary>
  605. /// <param name="nowtime">现在时间</param>
  606. /// <param name="kjtime">开奖时间</param>
  607. /// <param name="days">过期时间 增加的天数</param>
  608. /// <returns>时间差</returns>
  609. public static TimeSpan KjTimeDiff(DateTime nowtime, DateTime kjtime)
  610. {
  611. TimeSpan ts = new TimeSpan();
  612. if (kjtime > nowtime)
  613. {
  614. TimeSpan ts1 = new TimeSpan(nowtime.Ticks);
  615. TimeSpan ts2 = new TimeSpan(kjtime.Ticks);
  616. ts = ts1.Subtract(ts2).Duration();
  617. }
  618. else
  619. {
  620. TimeSpan ts1 = new TimeSpan(nowtime.Ticks);
  621. TimeSpan ts2 = new TimeSpan(nowtime.Ticks);
  622. ts = ts1.Subtract(ts2).Duration();
  623. }
  624. return ts;
  625. }
  626. public static string GetLoadTitle(string title)
  627. {
  628. if (title.Length > 20)
  629. {
  630. title = title.Substring(0, 17).PadRight(20, '.');
  631. }
  632. return title;
  633. }
  634. /// <summary>
  635. /// 取页面源码
  636. /// </summary>
  637. /// <param name="url"></param>
  638. /// <returns></returns>
  639. public static string GetPagehtml(string url)
  640. {
  641. try
  642. {
  643. WebRequest request = WebRequest.Create(url);
  644. request.Timeout = 20000;//20秒超时
  645. WebResponse response = request.GetResponse();
  646. Stream resStream = response.GetResponseStream();
  647. StreamReader sr = new StreamReader(resStream);
  648. return sr.ReadToEnd();
  649. }
  650. catch { return ""; }
  651. }
  652. }
  653. }