/// <summary> /// 识别编码 /// </summary> /// <param name="objHttpItems"></param> private void GetEncodingNew(ref HttpItems objHttpItems) { string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length); Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline); string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty; if (charter.IndexOf("\"") > 0) { charter = charter.Split('\"')[0]; } if (charter.IndexOf(" ") > 0) { charter = charter.Split(' ')[0]; } charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty); if (charter.Length > 0) { charter = charter.ToLower().Replace("iso-8859-1", "gbk"); if (string.IsNullOrEmpty(response.CharacterSet.Trim()) || response.CharacterSet.Trim().Contains("utf")) { objHttpItems.Encoding = Encoding.UTF8; } else { Encoding ed = StreamEncoder.GetEncodingFromBytes(RawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } } else { if (response.CharacterSet != null) { if (response.CharacterSet.ToLower().Trim() == "iso-8859-1") { // encoding = Encoding.GetEncoding("gbk"); 直接改为UTF8编码 objHttpItems.Encoding = Encoding.GetEncoding("gbk");//Encoding.UTF8; } else { if (string.IsNullOrEmpty(response.CharacterSet.Trim()) || response.CharacterSet.Trim().Contains("utf")) { objHttpItems.Encoding = Encoding.UTF8; } else { objHttpItems.Encoding = Encoding.GetEncoding(response.CharacterSet); } } } else { Encoding ed = StreamEncoder.GetEncodingFromBytes(RawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } } }
/// <summary> /// 处理响应结果 /// </summary> /// <param name="objHttpItems">请求设置参数</param> /// <returns></returns> private HttpResults GetResponesInfo(ref HttpItems objHttpItems) { #region 设置返回结果 result.StatusCode = response.StatusCode; result.StatusDescription = response.StatusDescription; result.Header = response.Headers; if (response.Cookies != null) { result.CookieCollection = response.Cookies; } if (response.ResponseUri != null) { result.ResponseUrl = response.ResponseUri.ToString(); } if (response.Headers["set-cookie"] != null) { //清理垃圾Cookie信息 result.Cookie = new XJHTTP().ClearCookie(response.Headers["set-cookie"]); result.RawCookie = response.Headers["set-cookie"]; } //处理返回值Container result.Container = objHttpItems.Container; #endregion #region 急速请求,不解析结果数据 if (objHttpItems.ResultType == ResultType.So) { return(result); } #endregion #region 应答结果转换为内存数据 using (_stream = new MemoryStream()) { //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)); } else { _stream = GetMemoryStream(response.GetResponseStream()); } //获取Byte RawResponse = _stream.ToArray(); } #endregion #region 如果设置返回结果类型则返回Byte数据 if (objHttpItems.ResultType == ResultType.Byte) { result.ResultByte = RawResponse; return(result); } #endregion #region 处理编码结果 if (objHttpItems.Encoding == null)//如果用户没有指定编码格式 { try { Encoding ed = StreamEncoder.GetEncodingFromBytes(RawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { GetEncodingNew(ref objHttpItems); } } catch { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } //得到返回的HTML try { if (RawResponse.Length > 0) { result.Html = objHttpItems.Encoding.GetString(RawResponse); } else { result.Html = "HttpCode接收数据长度为0.请检查数据包.当前状态码:" + result.StatusCodeNum.ToString() + result.Html; } response.Close(); } catch { result.Html = "HttpCode接收数据出现异常..请检查数据包.当前状态码:" + result.StatusCodeNum.ToString() + result.Html; } #endregion return(result); }