/// <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> /// 获取远程页面的返回信息,只获取,不上载 /// </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> /// 获取指定路径下的模板的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> /// 获取远程页面的返回信息,以不同字符集并以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> /// 获得用户登录时的真实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); }