예제 #1
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
                result.CookieCollection = new CookieCollection();
                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;
                    return result;
                }
                //无视编码
                if (encoding == null)
                {
                    string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);
                    //<meta(.*?)charset([\s]?)=[^>](.*?)>
                    //<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
                    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").Split(' ')[0];
                        encoding = Encoding.GetEncoding(charter);
                    }
                    else
                    {

                        if (response.CharacterSet != null)
                        {
                            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
                try
                {
                    result.Html = encoding.GetString(RawResponse);
                    _stream.Close();
                    response.Close();
                }
                catch
                {
                    return null;
                }
                //最后释放流

                #endregion

            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                result.Html = "String Error";
                response = (HttpWebResponse)ex.Response;
                return result;
            }
            if (objhttpitem.IsToLower)
            {
                result.Html = result.Html;//.ToLower();
            }
            return result;
        }
예제 #2
0
 /// <summary>
 /// 获取验证码
 /// </summary>
 /// <param name="objhttpItem">参数列表</param>
 /// <returns>Img</returns>
 public Image GetImg(HttpResult hr)
 {
     byte[] bt = Encoding.Default.GetBytes(hr.Html);
     return byteArrayToImage(bt);
 }