Example #1
0
        public static string GetHTMLContent(string url, System.Text.Encoding encoding, string endRegexString)
        {
            HttpWebResponse httpWebResponse = null;

            System.IO.Stream       stream       = null;
            System.IO.StreamReader streamReader = null;
            string result;

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Timeout = 30000;
                httpWebResponse        = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    result = null;
                }
                else
                {
                    stream = httpWebResponse.GetResponseStream();
                    if (encoding == null)
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(httpWebResponse.CharacterSet) || httpWebResponse.CharacterSet == "ISO-8859-1")
                            {
                                encoding = HttpCollects.getEncoding(url);
                            }
                            else
                            {
                                encoding = System.Text.Encoding.GetEncoding(httpWebResponse.CharacterSet);
                            }
                        }
                        catch
                        {
                            encoding = System.Text.Encoding.UTF8;
                        }
                        if (encoding == null)
                        {
                            encoding = System.Text.Encoding.UTF8;
                        }
                    }
                    httpWebRequest.Timeout = 8000;
                    httpWebRequest         = (HttpWebRequest)WebRequest.Create(url);
                    httpWebResponse        = (HttpWebResponse)httpWebRequest.GetResponse();
                    stream       = httpWebResponse.GetResponseStream();
                    streamReader = new System.IO.StreamReader(stream, encoding);
                    string text;
                    if (string.IsNullOrEmpty(endRegexString))
                    {
                        text = streamReader.ReadToEnd();
                    }
                    else
                    {
                        Regex regex = new Regex(endRegexString, RegexOptions.IgnoreCase);
                        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                        string text2 = string.Empty;
                        while ((text2 = streamReader.ReadLine()) != null)
                        {
                            stringBuilder.Append(text2);
                            text2 = stringBuilder.ToString();
                            if (regex.IsMatch(text2))
                            {
                                break;
                            }
                        }
                        text = stringBuilder.ToString();
                    }
                    streamReader.Close();
                    stream.Close();
                    httpWebResponse.Close();
                    result = text;
                }
            }
            catch (WebException)
            {
                result = null;
            }
            catch (System.IO.IOException)
            {
                result = null;
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
            }
            return(result);
        }