/// <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;
 }
Esempio n. 2
0
        /// <summary>
        /// 根据相传入的数据,得到相应页面数据
        /// </summary>
        /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>
        /// <returns>string类型的响应数据</returns>
        private HttpResult GetHttpRequestData(HttpItem objhttpitem)
        {
            //返回参数
            HttpResult result = new HttpResult();
            try
            {
                #region 得到请求的response
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    result.Header = response.Headers;
                    if (response.Cookies != null)
                    {
                        result.CookieCollection = response.Cookies;
                    }
                    if (response.Headers["set-cookie"] != null)
                    {
                        result.Cookie = response.Headers["set-cookie"];
                    }

                    MemoryStream _stream = new MemoryStream();
                    //GZIIP处理
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //开始读取流并设置编码方式
                        //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                    }
                    else
                    {
                        //开始读取流并设置编码方式
                        //response.GetResponseStream().CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(response.GetResponseStream());
                    }
                    //获取Byte
                    byte[] RawResponse = _stream.ToArray();
                    //是否返回Byte类型数据
                    if (objhttpitem.ResultType == ResultType.Byte)
                    {
                        result.ResultByte = RawResponse;
                    }
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                    {
                        string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);
                        //<meta(.*?)charset([\s]?)=[^>](.*?)>
                        Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty;
                        charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty);
                        if (charter.Length > 0)
                        {
                            charter = charter.ToLower().Replace("iso-8859-1", "gbk");
                            encoding = Encoding.GetEncoding(charter);
                        }
                        else
                        {
                            if (response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.GetEncoding("gbk");
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                    }
                    //得到返回的HTML
                    result.Html = encoding.GetString(RawResponse);
                    //最后释放流
                    _stream.Close();
                }
                #endregion
            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                result.Html = "String Error";
                response = (HttpWebResponse)ex.Response;
            }
            if (objhttpitem.IsToLower)
            {
                result.Html = result.Html.ToLower();
            }
            return result;
        }
Esempio n. 3
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);
             }
         }
     }
 }
Esempio n. 4
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;
            //获取最后访问的URl
            result.ResponseUri = response.ResponseUri.ToString();
            //获取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
        }
Esempio n. 5
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 = new HttpResult();
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "配置参数时出错:" + ex.Message;
         return result;
     }
     try
     {
         #region 得到请求的response
         using (response = (HttpWebResponse)request.GetResponse())
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
             result.Header = response.Headers;
             if (response.Cookies != null) result.CookieCollection = response.Cookies;
             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
             MemoryStream _stream = new MemoryStream();
             //GZIIP处理
             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
             {
                 //开始读取流并设置编码方式
                 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
             }
             else
             {
                 //开始读取流并设置编码方式
                 //response.GetResponseStream().CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(response.GetResponseStream());
             }
             //获取Byte
             byte[] ResponseByte = _stream.ToArray();
             _stream.Close();
             if (ResponseByte != null & ResponseByte.Length > 0)
             {
                 //是否返回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 = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty;
                     if (c.Length > 2)
                     {
                         try
                         {
                             if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" "));
                             encoding = Encoding.GetEncoding(c.Replace("\"", "").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);
                     }
                 }
                 //得到返回的HTML
                 result.Html = encoding.GetString(ResponseByte);
             }
             else
             {
                 //得到返回的HTML
                 result.Html = "本次请求并未返回任何数据";
             }
         }
         #endregion
     }
     catch (WebException ex)
     {
         //这里是在发生异常时返回的错误信息
         response = (HttpWebResponse)ex.Response;
         result.Html = ex.Message;
         if (response != null)
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }