예제 #1
0
        /// <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;
                    }
                }
            }
        }
예제 #2
0
        /// <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);
        }