Example #1
0
 /// <summary>  
 /// 设置编码  
 /// </summary>  
 /// <param name="item">HttpItem</param>  
 /// <param name="result">HttpResult</param>  
 /// <param name="responseByte">byte[]</param>  
 private void SetEncoding(HttpItem item, HttpResult result, byte[] responseByte)
 {
     //是否返回Byte类型数据
     if (item.ResultType == ResultType.Byte) result.ResultByte = responseByte;
     //从这里开始我们要无视编码了
     if (_encoding == null)
     {
         Match meta = Regex.Match(Encoding.Default.GetString(responseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
         string c = string.Empty;
         if (meta != null && meta.Groups.Count > 0)
         {
             c = meta.Groups[1].Value.ToLower().Trim();
         }
         if (c.Length > 2)
         {
             try
             {
                 _encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
             }
             catch
             {
                 if (string.IsNullOrEmpty(_response.CharacterSet))
                 {
                     _encoding = Encoding.UTF8;
                 }
                 else
                 {
                     _encoding = Encoding.GetEncoding(_response.CharacterSet);
                 }
             }
         }
         else
         {
             if (string.IsNullOrEmpty(_response.CharacterSet))
             {
                 _encoding = Encoding.UTF8;
             }
             else
             {
                 _encoding = Encoding.GetEncoding(_response.CharacterSet);
             }
         }
     }
 }
Example #2
0
 /// <summary>  
 /// 根据相传入的数据,得到相应页面数据  
 /// </summary>  
 /// <param name="item">参数类对象</param>  
 /// <returns>返回HttpResult类型</returns>  
 public HttpResult GetHtml(HttpItem item)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(item);
     }
     catch (Exception ex)
     {
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "配置参数时出错:" + ex.Message;
         //配置参数时出错
         return result;
     }
     try
     {
         //请求数据
         using (_response = (HttpWebResponse)_request.GetResponse())
         {
             GetData(item, result);
         }
     }
     catch (WebException ex)
     {
         if (ex.Response != null)
         {
             using (_response = (HttpWebResponse)ex.Response)
             {
                 GetData(item, result);
             }
         }
         else
         {
             result.Html = ex.Message;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Example #3
0
        /// <summary>  
        /// 获取数据的并解析的方法  
        /// </summary>  
        /// <param name="item"></param>  
        /// <param name="result"></param>  
        private void GetData(HttpItem item, HttpResult result)
        {
            #region base
            //获取StatusCode
            result.StatusCode = _response.StatusCode;
            //获取StatusDescription
            result.StatusDescription = _response.StatusDescription;
            //获取Headers
            result.Header = _response.Headers;
            //获取CookieCollection
            if (_response.Cookies != null) result.CookieCollection = _response.Cookies;
            //获取set-cookie
            if (_response.Headers["set-cookie"] != null) result.Cookie = _response.Headers["set-cookie"];
            #endregion

            #region byte
            //处理网页Byte
            byte[] responseByte = GetByte();
            #endregion

            #region Html
            if (responseByte != null && responseByte.Length > 0)
            {
                //设置编码
                SetEncoding(item, result, responseByte);
                //得到返回的HTML
                result.Html = _encoding.GetString(responseByte);
            }
            else
            {
                //没有返回任何Html代码
                result.Html = string.Empty;
            }
            #endregion
        }