Ejemplo n.º 1
0
        public static string HttpGet(string Url, Encoding encode)//Http Get方法
        {
            string         retString = "";
            HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(Url);

            request.Method = "GET";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            request.Headers.Add("Accept-Encoding:gzip, deflate");//启用压缩编码
            request.Headers.Add("Cache-Control:max-age=0");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
            request.KeepAlive = true;
            request.Host      = new Uri(Url).Host;
            HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
            Stream          myResponseStream = response.GetResponseStream();

            switch (response.ContentEncoding)
            {
            case "gzip": retString = DecompressEncode.DecompressGzip(myResponseStream, encode); break;

            case "deflate": retString = DecompressEncode.DecompressDeflate(myResponseStream, encode); break;

            default: retString = DecompressEncode.NoCompress(myResponseStream, encode); break;
            }

            return(retString);
        }
Ejemplo n.º 2
0
        public Stream DownLoad(string url, string referUrl)//下载模块,添加gzip支持
        {
            Stream         imgstream = null;
            HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(url);

            request.Accept    = "image/webp,image/*,*/*;q=0.8";
            request.Method    = "GET";
            request.KeepAlive = true;
            request.Host      = new Uri(url).Host;
            request.Headers.Add("Accept-Encoding:gzip, deflate");

            if (request.Host == "imgn1.magentozh.com")
            {
                request.Referer = "http://www.verydm.com/";
            }
            else if (referUrl.Contains("-") == false)
            {
                request.Referer = referUrl;
            }

            request.Headers.Add("Cache-Control:max-age=0");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
            request.Timeout   = 10000;

            try
            {
                HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
                imgstream = resp.GetResponseStream();

                switch (resp.ContentEncoding)
                {
                case "gzip": imgstream = DecompressEncode.DecompressGzipStream(imgstream); break;

                case "deflate":; break;
                }

                return(imgstream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("-----下载超时------");
                return(imgstream);
            }
        }
Ejemplo n.º 3
0
        public static string HttpGet(string Url, string referUrl)//Http Get方法
        {
            Encoding encode;
            string   retString = "";

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
                    ServicePointManager.CheckCertificateRevocationList = true;
                    ServicePointManager.DefaultConnectionLimit         = 100;
                    ServicePointManager.Expect100Continue = false;
                }

                request.Method = "GET";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                request.Headers.Add("Accept-Encoding:gzip, deflate");//启用压缩编码
                request.Headers.Add("Cache-Control:max-age=0");
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
                request.KeepAlive = false;
                request.Referer   = referUrl;
                request.Host      = new Uri(Url).Host;
                request.Timeout   = 5000;

                encode = Encoding.UTF8;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                switch (response.CharacterSet)
                {
                case "ISO-8859-1":
                    if (request.Host != "www.dmzx.com")
                    {
                        encode = Encoding.UTF8;
                    }
                    else
                    {
                        encode = Encoding.GetEncoding("gb2312");
                    }
                    break;

                case "gb2312": encode = Encoding.GetEncoding("gb2312"); break;

                default: break;
                }

                Stream myResponseStream = response.GetResponseStream();

                switch (response.ContentEncoding)
                {
                case "gzip": retString = DecompressEncode.DecompressGzip(myResponseStream, encode); break;

                case "deflate": retString = DecompressEncode.DecompressDeflate(myResponseStream, encode); break;

                default: retString = DecompressEncode.NoCompress(myResponseStream, encode); break;
                }
            }
            catch (Exception ex)
            {
                retString = "";
            }
            return(retString);
        }