/// <summary> /// 获取远程页面的返回信息,只获取,不上载 /// </summary> /// <param name="url">带请求参数的url地址</param> /// <param name="encoding">编码方式</param> /// <returns>响应后的资源</returns> public static string GetHttpResponse(string url, Encoding encoding) { if (string.IsNullOrEmpty(url)) { throw new UrlIsNullOrEmptyException(); } if (!RegexService.IsMatch(url, RegexPattern.Url)) { throw new UrlFormatIsErrorException(); } if (encoding == null) { throw new EncodingIsNullException(); } string strReturnString = null; using (WebClient webClient = new WebClient()) { webClient.Credentials = CredentialCache.DefaultCredentials; webClient.Encoding = encoding; strReturnString = webClient.DownloadString(url); } return(strReturnString); }
/// <summary> /// 获取远程页面的返回信息,以XML格式上载后获取 /// </summary> /// <param name="url">资源的URL</param> /// <param name="parameter">要上载的字符串</param> /// <returns>响应后的资源</returns> public static string GetHttpResponse(string url, string parameter) { if (string.IsNullOrEmpty(url)) { throw new UrlIsNullOrEmptyException(); } if (!RegexService.IsMatch(url, RegexPattern.Url)) { throw new UrlFormatIsErrorException(); } if (string.IsNullOrEmpty(parameter)) { throw new UploadParamsIsNullOrEmptyException(); } string strReturnString = null; using (WebClient webClient = new WebClient()) { webClient.Credentials = CredentialCache.DefaultCredentials; webClient.Encoding = Encoding.Default; webClient.Headers.Add(HttpRequestHeader.ContentType, "text/xml"); strReturnString = webClient.UploadString(url, parameter); } return(strReturnString); }
/// <summary> /// 获取指定路径下的模板的HTML源代码 /// </summary> /// <param name="templateUrlPath">模板的路径(URL格式的路径)</param> /// <param name="encoding">网页类型(有些是UTF8,有些是GB2312)</param> /// <returns>源代码</returns> public static string GetHtmlCode(string templateUrlPath, Encoding encoding) { if (string.IsNullOrEmpty(templateUrlPath)) { throw new UrlIsNullOrEmptyException(); } if (!RegexService.IsMatch(templateUrlPath, RegexPattern.Url)) { throw new UrlFormatIsErrorException(); } if (encoding == null) { throw new EncodingIsNullException(); } string htmlCode = ""; htmlCode = GetHttpResponse(templateUrlPath, encoding); htmlCode = Regex.Replace(htmlCode, @"<!DOCTYPE\s*HTML\s*PUBLIC[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled); return(htmlCode); }
/// <summary> /// 正则表达式匹配,返回是否匹配成功,只匹配从头到尾的方式 /// </summary> /// <param name="input">要匹配的字符串</param> /// <param name="pattern">正则表达式</param> /// <returns>true表示匹配正确,false表示匹配失败</returns> public static bool IsMatch(string input, RegexPattern pattern) { string regexPattern = string.Empty; regexPattern = RegexService.GetPattern(pattern); return(IsMatch(input, regexPattern)); }
/// <summary> /// 获取远程页面的返回信息,以不同字符集并以XML格式上载后获取,在请求的头部加上MD5的校验 /// </summary> /// <param name="url">请求页面地址</param> /// <param name="parameter">请求提交数据</param> /// <param name="encoding">编码方式</param> /// <param name="contentMd5">Content-MD5头域</param> /// <returns>返回页面内容</returns> public static string GetHttpResponse(string url, string parameter, Encoding encoding, string contentMd5) { if (string.IsNullOrEmpty(url)) { throw new UrlIsNullOrEmptyException(); } if (!RegexService.IsMatch(url, RegexPattern.Url)) { throw new UrlFormatIsErrorException(); } if (string.IsNullOrEmpty(parameter)) { throw new UploadParamsIsNullOrEmptyException(); } if (encoding == null) { throw new EncodingIsNullException(); } if (string.IsNullOrEmpty(contentMd5)) { throw new ContentMD5IsNullOrEmptyException(); } string strRespData = null; using (WebClient objWClient = new WebClient()) { objWClient.Credentials = CredentialCache.DefaultCredentials; objWClient.Encoding = encoding; objWClient.Headers.Add(HttpRequestHeader.ContentType, "text/xml"); objWClient.Headers.Add(HttpRequestHeader.ContentMd5, contentMd5); if (parameter.Length > 0) { strRespData = objWClient.UploadString(url, parameter); } else { strRespData = objWClient.DownloadString(url); } } return(strRespData); }
/// <summary> /// 获取正则表达式 /// </summary> /// <param name="pattern">正则表达式</param> /// <returns>正则表达式</returns> internal static string GetPattern(RegexPattern pattern) { string regexPattern = string.Empty; switch (pattern) { case RegexPattern.Mobile: regexPattern = RegexService.GetMobileRegex(); break; case RegexPattern.Mail139Alias: regexPattern = RegexService.GetMail139AliasRegex(); break; case RegexPattern.Mobile139Alias: regexPattern = RegexService.GetMobile139AliasRegex(); break; case RegexPattern.MailPassword: regexPattern = RegexService.GetMailPasswordRegex(); break; case RegexPattern.MailAddress: regexPattern = RegexService.GetMailAddressRegex(); break; case RegexPattern.ChinaPhone: regexPattern = RegexService.GetChinaPhoneRegex(); break; case RegexPattern.ChinaIDCard: regexPattern = RegexService.GetChinaIDCardRegex(); break; case RegexPattern.ZipCode: regexPattern = RegexService.GetZipCodeRegex(); break; case RegexPattern.IpAddress: regexPattern = RegexService.GetIpAddressRegex(); break; case RegexPattern.Url: regexPattern = RegexService.GetUrlRegex(); break; case RegexPattern.FileName: regexPattern = RegexService.GetFileNameRegex(); break; case RegexPattern.Path: regexPattern = RegexService.GetPathRegex(); break; case RegexPattern.NumberComma: regexPattern = RegexService.GetNumberCommaRegex(); break; case RegexPattern.Number: regexPattern = RegexService.GetNumberRegex(); break; case RegexPattern.Chinese: regexPattern = RegexService.GetChineseRegex(); break; case RegexPattern.Letter: regexPattern = RegexService.GetLetterRegex(); break; case RegexPattern.LetterNumberUnderline: regexPattern = RegexService.GetLetterNumberUnderlineRegex(); break; case RegexPattern.yyyyMM: regexPattern = RegexService.GetYYYYMMRegex(); break; case RegexPattern.SBCCheck: regexPattern = RegexService.GetSBCRegex(); break; case RegexPattern.SpNumber: regexPattern = RegexService.GetSpNumberRegex(); break; case RegexPattern.VerifyCode: regexPattern = RegexService.GetVerifyCodeRegex(); break; } return(regexPattern); }
/// <summary> /// 获得用户登录时的真实IP /// </summary> /// <returns>客户端IP地址</returns> public static string GetClientIP() { string result = "未知"; try { string httpXForwardedFor = HttpContext.Current.Request. ServerVariables["HTTP_X_FORWARDED_FOR"] as string; if (httpXForwardedFor != null && httpXForwardedFor.Trim().Length > 0) { string[] strFromIP = httpXForwardedFor.Trim().Split(','); for (int i = 0; i < strFromIP.Length; i++) { if (RegexService.IsMatch( strFromIP[i], RegexPattern.IpAddress)) { result = strFromIP[i]; break; } } } string httpVia = HttpContext.Current.Request.ServerVariables["HTTP_VIA"] as string; if (result == "未知" && httpVia != null && httpVia.Trim().Length > 0) { result = httpVia; } string remoteAddr = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] as string; if (result == "未知" && remoteAddr != null && remoteAddr.Trim().Length > 0) { result = remoteAddr; } if (result == "未知" && HttpContext.Current.Request.UserHostName.Trim().Length > 0) { if (RegexService.IsMatch(HttpContext.Current.Request.UserHostName, RegexPattern.IpAddress)) { result = HttpContext.Current.Request.UserHostName; } else { IPAddress[] ipAddr = Dns.GetHostAddresses(HttpContext.Current.Request.UserHostName); for (int i = 0; i < ipAddr.Length; i++) { if (RegexService.IsMatch(ipAddr[i].ToString(), RegexPattern.IpAddress)) { result = ipAddr[i].ToString(); break; } } } } } catch { result = "未知"; } return(result); }