Example #1
0
        public override HtmlDocument Load(string url, uint level, string path, object userData, int tryCount)
        {
            var client = new MyWebClient();
            var data = LoadData(client, url, level, tryCount);
            if (data == null)
                return null;

            Encoding charset = null;
            var ct = client.ResponseHeaders["Content-Type"];
            var ex = new Regex("charset=(.+)");
            var match = ex.Match(ct);
            if (match.Groups.Count == 2)
            {
                charset = Encoding.GetEncoding(match.Groups[1].Value);
            }

            var web = new HtmlDocument();
            Encoding encoding = null;
            using (MemoryStream ms = new MemoryStream(data))
            {
                encoding = web.DetectEncoding(ms);
                if (encoding == null)
                    encoding = charset;
                if (encoding == null)
                    encoding = Encoding.UTF8;
            }

            if (charset != null && charset != encoding)
            {
                data = Encoding.Convert(charset, encoding, data);
            }

            using (MemoryStream ms = new MemoryStream(data))
            {
                web.Load(ms, encoding);
            }

            return web;
        }
Example #2
0
        protected virtual byte[] LoadData(ref string filename, string url, uint level, int tryCount)
        {
            var client = new MyWebClient();
            var data = LoadData(client, url, level, tryCount);

            var dis = client.ResponseHeaders["Content-Disposition"];
            if (String.IsNullOrWhiteSpace(dis))
                return data;

            try
            {
                filename = AssemblyHelper.ParseString("filename=(.*)", dis);
            }
            catch
            {
            }

            return data;
        }