自动编码获取页面html代码

   /// <summary>
        ///获取http流字符串
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string GetHttpData(string Url)
        {

   /// <summary>
        ///获取http流字符串
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string GetHttpData(string Url)
        {


            string code = DecodeData(Url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Timeout = 30000;
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = code != string.Empty ? Encoding.GetEncoding(code.ToUpper()) : Encoding.Default;
            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string strResult = streamReader.ReadToEnd();
            streamReader.Close();
            streamReader.Dispose();

            return strResult;


        }
        private static string DecodeData(string Url)
        {
            WebRequest r = WebRequest.Create(Url);
            WebResponse w = r.GetResponse();
            //    
            //   first   see   if   content   length   header   has   charset   =   calue    
            //    
            String charset = string.Empty;
            String ctype = w.Headers["content-type"];
            if (ctype != null)
            {
                int ind = ctype.IndexOf("charset=");
                if (ind != -1)
                {
                    charset = ctype.Substring(ind + 8);
                }
            }

            //   save   data   to   a   memorystream    
            MemoryStream rawdata = new MemoryStream();
            byte[] buffer = new byte[1024];
            Stream rs = w.GetResponseStream();
            int read = rs.Read(buffer, 0, buffer.Length);
            while (read > 0)
            {
                rawdata.Write(buffer, 0, read);
                read = rs.Read(buffer, 0, buffer.Length);
            }

            rs.Close();

            //    
            //   if   ContentType   is   null,   or   did   not   contain   charset,   we   search   in   body    
            //    
            if (charset == null)
            {
                MemoryStream ms = rawdata;
                ms.Seek(0, SeekOrigin.Begin);

                StreamReader srr = new StreamReader(ms, Encoding.ASCII);
                String meta = srr.ReadToEnd();

                if (meta != null)
                {
                    int start_ind = meta.IndexOf("charset=");
                    int end_ind = -1;
                    if (start_ind != -1)
                    {
                        end_ind = meta.IndexOf("/"", start_ind);
                        if (end_ind != -1)
                        {
                            int start = start_ind + 8;
                            charset = meta.Substring(start, end_ind - start + 1);
                            charset = charset.TrimEnd(new Char[] { '>', '"' });
                        }
                    }
                }
            }
            return charset.ToString();
        } 


            string code = DecodeData(Url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Timeout = 30000;
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = code != string.Empty ? Encoding.GetEncoding(code.ToUpper()) : Encoding.Default;
            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string strResult = streamReader.ReadToEnd();
            streamReader.Close();
            streamReader.Dispose();

            return strResult;


        }
        private static string DecodeData(string Url)
        {
            WebRequest r = WebRequest.Create(Url);
            WebResponse w = r.GetResponse();
            //    
            //   first   see   if   content   length   header   has   charset   =   calue    
            //    
            String charset = string.Empty;
            String ctype = w.Headers["content-type"];
            if (ctype != null)
            {
                int ind = ctype.IndexOf("charset=");
                if (ind != -1)
                {
                    charset = ctype.Substring(ind + 8);
                }
            }

            //   save   data   to   a   memorystream    
            MemoryStream rawdata = new MemoryStream();
            byte[] buffer = new byte[1024];
            Stream rs = w.GetResponseStream();
            int read = rs.Read(buffer, 0, buffer.Length);
            while (read > 0)
            {
                rawdata.Write(buffer, 0, read);
                read = rs.Read(buffer, 0, buffer.Length);
            }

            rs.Close();

            //    
            //   if   ContentType   is   null,   or   did   not   contain   charset,   we   search   in   body    
            //    
            if (charset == null)
            {
                MemoryStream ms = rawdata;
                ms.Seek(0, SeekOrigin.Begin);

                StreamReader srr = new StreamReader(ms, Encoding.ASCII);
                String meta = srr.ReadToEnd();

                if (meta != null)
                {
                    int start_ind = meta.IndexOf("charset=");
                    int end_ind = -1;
                    if (start_ind != -1)
                    {
                        end_ind = meta.IndexOf("/"", start_ind);
                        if (end_ind != -1)
                        {
                            int start = start_ind + 8;
                            charset = meta.Substring(start, end_ind - start + 1);
                            charset = charset.TrimEnd(new Char[] { '>', '"' });
                        }
                    }
                }
            }
            return charset.ToString();
        } 

猜你喜欢

转载自blog.csdn.net/flon/article/details/5163172
今日推荐