C#使用正则实现条件检查

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Text.RegularExpressions;
  5 using System.Data;
  6 using System.Reflection;
  7 using Com.Vanke.HR.Business;
  8 using Com.Vanke.HR.InfoTypes.HumanCenter;
  9 using System.Net;
 10 using System.IO;
 11 using System.Web;
 12 
 13 namespace Com.Vanke.HR.Util
 14 {
 15     public class Common
 16     {
 17         /// <summary>
 18         /// 判断N位数值
 19         /// </summary>
 20         /// <param name="input"></param>
 21         /// <returns></returns>
 22         public static bool CheckNumberBits(string input, int length)
 23         {
 24             Regex reg = new Regex("^[0-9]{" + length.ToString().Trim() + "}$");
 25             Match ma = reg.Match(input);
 26             if (ma.Success)
 27             {
 28                 return true;
 29             }
 30             else
 31             {
 32                 return false;
 33             }
 34         }
 35         /// <summary>
 36         /// 判断输入是否数值
 37         /// </summary>
 38         /// <param name="input"></param>
 39         /// <returns></returns>
 40         public static bool CheckIsNumber(string input)
 41         {
 42             Regex reg = new Regex("^[0-9]+$");
 43             Match ma = reg.Match(input);
 44             if (ma.Success)
 45             {
 46                 return true;
 47             }
 48             else
 49             {
 50                 return false;
 51             }
 52         }
 53         //判断字符串是否为浮点数
 54         public static bool IsFloat(string str)
 55         {
 56             string regextext = @"^(-?\d+)(\.\d+)?$";
 57             Regex regex = new Regex(regextext, RegexOptions.None);
 58             return regex.IsMatch(str.Trim());
 59         }
 60         //  查找两个字符串之间的字符串
 61         /// <summary>
 62         /// 查找两个字符串之间的字符串
 63         /// </summary>
 64         /// <param name="str"></param>
 65         /// <param name="a"></param>
 66         /// <param name="b"></param>
 67         /// <returns></returns>
 68         public static string GetByTwoString(string str, string a, string b)
 69         {
 70             Regex re = new Regex(string.Format("(?<={0})[\\d\\D]+(?={1})", a, b));
 71             return re.Match(str).Value;
 72         }
 73         /// <summary>
 74         /// 检测输入字符串是否半角
 75         /// </summary>
 76         /// <param name="str"></param>
 77         /// <returns></returns>
 78         public static bool CheckDBCcase(string checkString)
 79         {
 80             return checkString.Length == Encoding.Default.GetByteCount(checkString);
 81         }
 82         /// <summary>
 83         /// 检测输入字符串是否全角
 84         /// </summary>
 85         /// <param name="str"></param>
 86         /// <returns></returns>
 87         public static bool CheckSBCcase(string checkString)
 88         {
 89             return 2 * checkString.Length == Encoding.Default.GetByteCount(checkString);
 90         }
 91         #region 全角半角转换
 92         /// <summary>
 93         /// 转全角的函数(SBC case)
 94         /// </summary>
 95         /// <param name="input">任意字符串</param>
 96         /// <returns>全角字符串</returns>
 97         ///<remarks>
 98         ///全角空格为12288,半角空格为32
 99         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
100         ///</remarks>
101         public static string ToSBC(string input)
102         {
103             //半角转全角:
104             char[] c = input.ToCharArray();
105             for (int i = 0; i < c.Length; i++)
106             {
107                 if (c[i] == 32)
108                 {
109                     c[i] = (char)12288;
110                     continue;
111                 }
112                 if (c[i] < 127)
113                     c[i] = (char)(c[i] + 65248);
114             }
115             return new string(c);
116         }
117         /// <summary> 转半角的函数(DBC case) </summary>
118         /// <param name="input">任意字符串</param>
119         /// <returns>半角字符串</returns>
120         ///<remarks>
121         ///全角空格为12288,半角空格为32
122         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
123         ///</remarks>
124         public static string ToDBC(string input)
125         {
126             char[] c = input.ToCharArray();
127             for (int i = 0; i < c.Length; i++)
128             {
129                 if (c[i] == 12288)
130                 {
131                     c[i] = (char)32;
132                     continue;
133                 }
134                 if (c[i] > 65280 && c[i] < 65375)
135                     c[i] = (char)(c[i] - 65248);
136             }
137             return new string(c);
138         }
139 
140         #endregion
141 
142         /// <summary>
143         /// 判断输入的字符串只包含汉字
144         /// </summary>
145         /// <param name="input"></param>
146         /// <returns></returns>
147         public static bool IsChineseCh(string input)
148         {
149             return IsMatch(@"^[\u4e00-\u9fa5]+$", input);
150         }
151 
152         /// <summary>
153         /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
154         /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
155         /// 也可以没有间隔
156         /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
157         /// </summary>
158         /// <param name="input"></param>
159         /// <returns></returns>
160         public static bool IsPhone(string input)
161         {
162             string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
163             return IsMatch(pattern, input);
164         }
165 
166         /// <summary>
167         /// 判断输入的字符串是否是一个合法的手机号
168         /// </summary>
169         /// <param name="input"></param>
170         /// <returns></returns>
171         public static bool IsMobilePhone(string input)
172         {
173             return IsMatch(@"^13\\d{9}$", input);
174         }
175 
176         /// <summary>
177         /// 判断输入的字符串只包含数字
178         /// 可以匹配整数和浮点数
179         /// ^-?\d+$|^(-?\d+)(\.\d+)?$
180         /// </summary>
181         /// <param name="input"></param>
182         /// <returns></returns>
183         public static bool IsNumber(string input)
184         {
185             string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
186             return IsMatch(pattern, input);
187         }
188 
189         /// <summary>
190         /// 匹配非负整数
191         /// </summary>
192         /// <param name="input"></param>
193         /// <returns></returns>
194         public static bool IsNotNagtive(string input)
195         {
196             return IsMatch(@"^\d+$", input);
197         }
198         /// <summary>
199         /// 匹配正整数
200         /// </summary>
201         /// <param name="input"></param>
202         /// <returns></returns>
203         public static bool IsUint(string input)
204         {
205             return IsMatch(@"^[0-9]*[1-9][0-9]*$", input);
206         }
207 
208         /// <summary>
209         /// 判断输入的字符串字包含英文字母
210         /// </summary>
211         /// <param name="input"></param>
212         /// <returns></returns>
213         public static bool IsEnglisCh(string input)
214         {
215             return IsMatch(@"^[A-Za-z]+$", input);
216         }
217 
218         /// <summary>
219         /// 判断输入的字符串是否是一个合法的Email地址
220         /// </summary>
221         /// <param name="input"></param>
222         /// <returns></returns>
223         public static bool IsEmail(string input)
224         {
225             string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
226             return IsMatch(pattern, input);
227         }
228 
229         /// <summary>
230         /// 判断输入的字符串是否只包含数字和英文字母
231         /// </summary>
232         /// <param name="input"></param>
233         /// <returns></returns>
234         public static bool IsNumAndEnCh(string input)
235         {
236             return IsMatch(@"^[A-Za-z0-9]+$", input);
237         }
238 
239         /// <summary>
240         /// 判断输入的字符串是否是一个超链接
241         /// </summary>
242         /// <param name="input"></param>
243         /// <returns></returns>
244         public static bool IsURL(string input)
245         {
246             string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
247             return IsMatch(pattern, input);
248         }
249 
250         /// <summary>
251         /// 判断输入的字符串是否是表示一个IP地址
252         /// </summary>
253         /// <param name="input">被比较的字符串</param>
254         /// <returns>是IP地址则为True</returns>
255         public static bool IsIPv4(string input)
256         {
257             string[] IPs = input.Split('.');
258 
259             for (int i = 0; i < IPs.Length; i++)
260             {
261                 if (!IsMatch(@"^\d+$", IPs[i]))
262                 {
263                     return false;
264                 }
265                 if (Convert.ToUInt16(IPs[i]) > 255)
266                 {
267                     return false;
268                 }
269             }
270             return true;
271         }
272 
273         /// <summary>
274         /// 判断输入的字符串是否是合法的IPV6 地址
275         /// </summary>
276         /// <param name="input"></param>
277         /// <returns></returns>
278         public static bool IsIPV6(string input)
279         {
280             string pattern = "";
281             string temp = input;
282             string[] strs = temp.Split(':');
283             if (strs.Length > 8)
284             {
285                 return false;
286             }
287             int count = GetStringCount(input, "::");
288             if (count > 1)
289             {
290                 return false;
291             }
292             else if (count == 0)
293             {
294                 pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
295                 return IsMatch(pattern, input);
296             }
297             else
298             {
299                 pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
300                 return IsMatch(pattern, input);
301             }
302         }
303 
304         #region 正则的通用方法
305         /// <summary>
306         /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
307         /// </summary>
308         /// <param name="input">需要计算的字符串</param>
309         /// <returns>返回字符串的长度</returns>
310         public static int GetCount(string input)
311         {
312             return Regex.Replace(input, @"[\u4e00-\u9fa5/g]", "aa").Length;
313         }
314 
315         /// <summary>
316         /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
317         /// </summary>
318         /// <param name="pattern">要匹配的正则表达式模式。</param>
319         /// <param name="input">要搜索匹配项的字符串</param>
320         /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>
321         public static bool IsMatch(string pattern, string input)
322         {
323             if (input == null || input == "") return false;
324             Regex regex = new Regex(pattern);
325             return regex.IsMatch(input);
326         }
327 
328         /// <summary>
329         /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
330         /// </summary>
331         /// <param name="pattern">模式字符串</param>
332         /// <param name="input">输入字符串</param>
333         /// <param name="replacement">用于替换的字符串</param>
334         /// <returns>返回被替换后的结果</returns>
335         public static string Replace(string pattern, string input, string replacement)
336         {
337             Regex regex = new Regex(pattern);
338             return regex.Replace(input, replacement);
339         }
340 
341         /// <summary>
342         /// 在由正则表达式模式定义的位置拆分输入字符串。
343         /// </summary>
344         /// <param name="pattern">模式字符串</param>
345         /// <param name="input">输入字符串</param>
346         /// <returns></returns>
347         public static string[] Split(string pattern, string input)
348         {
349             Regex regex = new Regex(pattern);
350             return regex.Split(input);
351         }
352 
353         /* *******************************************************************
354          * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
355          * 2、判断输入的IPV6字符串中是否有“::”。
356          * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
357          * 4、如果有“::” ,判断"::"是否止出现一次
358          * 5、如果出现一次以上 返回false
359          * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
360          * ******************************************************************/
361         /// <summary>
362         /// 判断字符串compare 在 input字符串中出现的次数
363         /// </summary>
364         /// <param name="input">源字符串</param>
365         /// <param name="compare">用于比较的字符串</param>
366         /// <returns>字符串compare 在 input字符串中出现的次数</returns>
367         private static int GetStringCount(string input, string compare)
368         {
369             int index = input.IndexOf(compare);
370             if (index != -1)
371             {
372                 return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
373             }
374             else
375             {
376                 return 0;
377             }
378 
379         }
380         /// <summary>
381         /// 是否为日期型字符串
382         /// </summary>
383         /// <param name="StrSource">日期字符串(2008-05-08)</param>
384         /// <returns></returns>
385         public static bool IsDate(string StrSource)
386         {
387             return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
388         }
389         /// <summary>
390         /// 是否为日期型字符串
391         /// </summary>
392         /// <param name="StrSource">日期字符串(2008-05-08)</param>
393         /// <returns></returns>
394         public static bool IsDate_yyyyMMdd(string StrSource)
395         {
396             //return Regex.IsMatch(StrSource, @"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)");
397             try
398             {
399                 string dte = string.Format("{0:yyyyMMdd}", (DateTime)Convert.ChangeType(StrSource.Trim(), typeof(DateTime)));
400             }
401             catch
402             {
403                 return false;
404             }
405             return true;
406         }
407 
408         /// <summary>
409         /// 是否为日期型字符串
410         /// </summary>
411         /// <param name="StrSource">日期字符串(2008-05)</param>
412         /// <returns></returns>
413         public static bool IsDate_yyyyMM(string StrSource)
414         {
415             try
416             {
417                 string dte = string.Format("{0:yyyyMMdd}", (DateTime)Convert.ChangeType(StrSource.Trim() + "-01", typeof(DateTime)));
418             }
419             catch
420             {
421                 return false;
422             }
423             return true;
424         }
425         /// <summary>
426         /// 是否为时间型字符串
427         /// </summary>
428         /// <param name="source">时间字符串(15:00:00)</param>
429         /// <returns></returns>
430         public static bool IsTime(string StrSource)
431         {
432             return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
433         }
434 
435         /// <summary>
436         /// 是否为日期+时间型字符串
437         /// </summary>
438         /// <param name="source"></param>
439         /// <returns></returns>
440         public static bool IsDateTime(string StrSource)
441         {
442             return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
443         }
444         #endregion
445 
446         public static string JoinBy(string[] from, string c, string splitCahr)
447         {
448             string split = c + splitCahr + c;
449             return c + string.Join(split, from) + c;
450         }
451         /// <summary>
452         /// 按长度分割字符串,汉字按一个字符算
453         /// </summary>
454         /// <param name="SourceString"></param>
455         /// <param name="Length"></param>
456         /// <returns></returns>
457         public static List<string> SplitByFixLength(string SourceString, int Length, int arrayLength)
458         {
459             List<string> DestString = new List<string>();
460             for (int i = 0; i < SourceString.Trim().Length; i += Length)
461             {
462                 if ((SourceString.Trim().Length - i) >= Length)
463                     DestString.Add(SourceString.Trim().Substring(i, Length));
464                 else
465                     DestString.Add(SourceString.Trim().Substring(i, SourceString.Trim().Length - i));
466             }
467             int l = DestString.Count;
468             for (int i = l; i < arrayLength; i++)
469             {
470                 DestString.Add("");
471             }
472             return DestString;
473         }
474         /// <summary>
475         /// 按长度分割字符串,汉字按一个字符算,返回第index个字符串
476         /// </summary>
477         /// <param name="SourceString"></param>
478         /// <param name="Length"></param>
479         /// <param name="index"></param>
480         /// <returns></returns>
481         public static string SplitByFixLengthIndex(string SourceString, int Length, int index)
482         {
483             if (index > (int)Math.Ceiling(SourceString.Length / (double)Length))
484             {
485                 return "";
486             }
487             else
488                 if (SourceString.Length > index * Length - Length && SourceString.Length <= index * Length)
489                     return SourceString.Substring(index * Length - Length);
490                 else
491                     return SourceString.Substring(SourceString.Length - Length, Length);
492 
493         }
494         /// <summary>
495         /// 添加测试项目至表格
496         /// </summary>
497         /// <param name="dr"></param>
498         /// <param name="ZTESTITEMNAME"></param>
499         /// <param name="ZSCORE"></param>
500         public static void AddTestItemToGrid(DataTable dt, string ZTESTITEMNAME, string ZSCORE)
501         {
502             if (ZTESTITEMNAME.Length > 0)
503             {
504                 DataRow dr = dt.NewRow();
505                 dr["ZTESTITEMNAME"] = ZTESTITEMNAME;//测试项目
506                 dr["ZSCORE"] = ZSCORE.TrimStart('0');//项目分数
507                 dt.Rows.Add(dr);
508             }
509         }
510         /// <summary>
511         /// 添加测试人至表格
512         /// </summary>
513         /// <param name="dr"></param>
514         /// <param name="ZTESTITEMNAME"></param>
515         /// <param name="ZSCORE"></param>
516         public static void AddItemviewerToGrid(DataTable dt, string Zinterviewer)
517         {
518             if (Zinterviewer.Length > 0)
519             {
520                 DataRow dr = dt.NewRow();
521                 dr["SapNO"] = Zinterviewer;//测试人
522                 if (Zinterviewer.Length > 0)
523                 {
524                     Employee employee = AppContainer<EmployeeService>.Instance.GetEmployee(Zinterviewer);
525                     if (employee != null)
526                     {
527                         dr["SName"] = employee.Name;//测试人
528                     }
529                 }
530                 dt.Rows.Add(dr);
531             }
532         }
533 
534         /// <summary>
535         /// 设置相应属性的值
536         /// </summary>
537         /// <param name="entity">实体</param>
538         /// <param name="fieldName">属性名</param>
539         /// <param name="fieldValue">属性值</param>
540         public static void SetValue(object entity, string fieldName, string fieldValue)
541         {
542             Type entityType = entity.GetType();
543             PropertyInfo propertyInfo = entityType.GetProperty(fieldName);
544             if (IsType(propertyInfo.PropertyType, "System.String"))
545             {
546                 propertyInfo.SetValue(entity, fieldValue, null);
547             }
548             if (IsType(propertyInfo.PropertyType, "System.Boolean"))
549             {
550                 propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
551             }
552             if (IsType(propertyInfo.PropertyType, "System.Int32"))
553             {
554                 if (fieldValue != "")
555                     propertyInfo.SetValue(entity, int.Parse(fieldValue), null);
556                 else
557                     propertyInfo.SetValue(entity, 0, null);
558             }
559             if (IsType(propertyInfo.PropertyType, "System.Decimal"))
560             {
561                 if (fieldValue != "")
562                     propertyInfo.SetValue(entity, Decimal.Parse(fieldValue), null);
563                 else
564                     propertyInfo.SetValue(entity, new Decimal(0), null);
565             }
566             if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
567             {
568                 if (fieldValue != "")
569                 {
570                     try
571                     {
572                         propertyInfo.SetValue(
573                             entity,
574                             (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
575                     }
576                     catch
577                     {
578                         propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
579                     }
580                 }
581                 else
582                     propertyInfo.SetValue(entity, null, null);
583             }
584         }
585 
586         /// <summary>
587         /// 类型匹配
588         /// </summary>
589         /// <param name="type"></param>
590         /// <param name="typeName"></param>
591         /// <returns></returns>
592         public static bool IsType(Type type, string typeName)
593         {
594             if (type.ToString() == typeName)
595                 return true;
596             if (type.ToString() == "System.Object")
597                 return false;
598 
599             return IsType(type.BaseType, typeName);
600         }
601         /// <summary>
602         /// 获取身份证的生日
603         /// </summary>
604         /// <param name="ICNUM"></param>
605         /// <returns></returns>
606         public static string GetBirthdayByICNUM(string ICNUM)
607         {
608             ICNUM = ICNUM.Trim().Replace(" ", "").Replace("  ", "");
609             //处理18位的身份证号码从号码中得到生日
610             if (ICNUM.Length == 18)
611             {
612                 return ICNUM.Substring(6, 4) + "-" + ICNUM.Substring(10, 2) + "-" + ICNUM.Substring(12, 2);
613 
614             }
615             if (ICNUM.Length == 15)
616             {
617                 return "19" + ICNUM.Substring(6, 2) + "-" + ICNUM.Substring(8, 2) + "-" + ICNUM.Substring(10, 2);
618             }
619             return "";
620         }
621         /// <summary>
622         /// 获取身份证的性别
623         /// </summary>
624         /// <param name="ICNUM"></param>
625         /// <returns></returns>
626         public static string GetSexByICNUM(string ICNUM)
627         {
628             ICNUM = ICNUM.Trim();
629             string sex = "";
630             //处理18位的身份证号码从号码中得到生日
631             if (ICNUM.Length == 18)
632             {
633                 sex = ICNUM.Substring(14, 3);
634             }
635             if (ICNUM.Length == 15)
636             {
637                 sex = ICNUM.Substring(12, 3);
638             }
639             //性别代码为偶数是女性奇数为男性
640             if (int.Parse(sex) % 2 == 0)
641             {
642                 return "";
643             }
644             else
645             {
646                 return "";
647             }
648         }
649         /// <summary>
650         /// 转换成日期
651         /// </summary>
652         /// <param name="number"></param>
653         /// <returns></returns>
654         public static string ConvertNumberToDate(string number)
655         {
656             if (number.Length == 0) return string.Empty;
657             number = number.Substring(0, 4) + "-" + number.Substring(4, 2) + "-" + number.Substring(6);
658             return number;
659         }
660         /// <summary>
661         /// 从身份证获取生日
662         /// </summary>
663         /// <param name="IDCard"></param>
664         /// <returns></returns>
665         public static string GetBirthday(string IDCard)
666         {
667             string birthday = "";
668             //二代身份证
669             if (IDCard.Length == 18)
670             {
671                 birthday = IDCard.Substring(6, 8);
672             }
673             //一代身份证
674             if (IDCard.Length == 15)
675             {
676                 birthday = "19" + IDCard.Substring(6, 6);
677             }
678             return birthday;
679         }
680         /// <summary>
681         /// 获取网页内容
682         /// </summary>
683         /// <param name="url"></param>
684         /// <returns></returns>
685         public static string GetUrl(string url)
686         {
687             string result = "";
688             try
689             {
690                 WebRequest request = WebRequest.Create(url);
691                 request.Credentials = new System.Net.NetworkCredential("S-HR-Service", "shs.123");//, "vanke");
692                 WebResponse response = request.GetResponse();
693                 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
694 
695                 result = reader.ReadToEnd();
696 
697                 reader.Close();
698                 reader.Dispose();
699                 response.Close();
700             }
701             catch (Exception ex)
702             {
703             }
704             return result;
705         }
706     }
707 }
View Code

 

posted @ 2016-08-03 15:25  微笑代表淡定.Net  阅读(111)  评论(0)    收藏  举报