Ejemplo n.º 1
0
        MemoryStream Download(Uri url, string referer, IList <RemoteCookie> cookies)
        {
            var wr  = WebRequest.Create(url);
            var hwr = wr as HttpWebRequest;

            if (hwr != null)
            {
                hwr.UserAgent = BrowserUserAgent;

                if (!string.IsNullOrWhiteSpace(referer))
                {
                    hwr.Referer = referer;
                }

                if (cookies != null && cookies.Count > 0)
                {
                    hwr.CookieContainer = new CookieContainer();
                    foreach (var remoteCookie in cookies)
                    {
                        try
                        {
                            var c = new Cookie(remoteCookie.Name, remoteCookie.Content, remoteCookie.Path);
                            hwr.CookieContainer.Add(url, c);
                        }
                        catch { }
                    }
                }
            }

            var resData = new MemoryStream();

            try
            {
                var res = wr.GetResponse();
                using (var resNetData = res.GetResponseStream())
                {
                    StreamEx.CopyFromNetStream(resNetData, resData, false, true);
                }

                return(resData);
            }
            catch (Exception)
            {
                resData.Dispose();
                throw;
            }
        }