Inheritance: Stream, IDisposable
Exemplo n.º 1
0
        public static async Task<WebStream> CreateStream(string url, string postData, string encoding,
            string postencoding = "application/x-www-form-urlencoded", string uagent = null,
            NameValueCollection headers = null, CookieCollection cookies = null, int timeout = 10000,
            bool redirect = true, string referer = null, IWebProxy proxy = null)
        {
            WebStream wb = new WebStream();
            try
            {
                do
                {
                    Uri u = new Uri(url);
                    if (string.IsNullOrEmpty(encoding))
                        encoding = "UTF-8";
                    int t = ServicePointManager.DefaultConnectionLimit;
                    Uri bas = new Uri(u.Scheme + "://" + u.Host);
                    HttpRequestMessage msg =
                        new HttpRequestMessage(string.IsNullOrEmpty(postData) ? HttpMethod.Get : HttpMethod.Post, u);
                    wb.DisposableObjects.Add(msg);
                    if (!string.IsNullOrEmpty(uagent))
                        msg.Headers.UserAgent.ParseAdd(uagent);
                    if (!string.IsNullOrEmpty(postData))
                    {
                        byte[] dta = Encoding.GetEncoding(encoding).GetBytes(postData);
                        msg.Content = new ByteArrayContent(dta, 0, dta.Length);
                        msg.Content.Headers.ContentType = new MediaTypeHeaderValue(postencoding);
                    }
                    if (headers != null)
                        PopulateHeaders(msg, headers);
                    if (referer != null)
                        msg.Headers.Referrer = new Uri(referer);
                    HttpClientHandler handler = new HttpClientHandler();
                    wb.DisposableObjects.Add(handler);
                    handler.AllowAutoRedirect = false;
                    handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                    if (cookies != null && cookies.Count > 0)
                    {
                        handler.CookieContainer = new CookieContainer();
                        foreach (Cookie c in cookies)
                        {
                            if (string.IsNullOrEmpty(c.Domain))
                                c.Domain = bas.Host;

                            handler.CookieContainer.Add(c);
                        }
                    }
                    if (proxy != null)
                        handler.Proxy = proxy;
                    HttpClient http = new HttpClient(handler);
                    wb.DisposableObjects.Add(http);
                    http.Timeout = TimeSpan.FromMilliseconds(timeout);
                    HttpResponseMessage response = await http.SendAsync(msg);
                    wb.DisposableObjects.Add(response);
                    wb.DisposableObjects.Add(response.Content);
                    wb.baseStream = await response.Content.ReadAsStreamAsync();
                    wb.ContentType = response.Content.Headers.ContentType.MediaType;
                    wb.ContentEncoding = response.Content.Headers.ContentEncoding.ToString();
                    wb.ContentLength = response.Content.Headers.ContentLength ?? 0;
                    wb.Headers = new NameValueCollection();
                    if (response.Headers.Contains("Set-Cookie"))
                    {
                        try
                        {
                            IEnumerable<string> sss=null;
                            response.Headers.TryGetValues("Set-Cookie", out sss);
                            wb.Cookies=new CookieCollection();
                            CookieCollection coll = null;
                            if (sss != null)
                            {
                                wb.Cookies = GetAllCookiesFromHeader(sss, bas.ToString());
                            }
                            if (cookies != null && cookies.Count > 0)
                            {
                                foreach (Cookie c in cookies)
                                {
                                    bool found = false;
                                    foreach (Cookie d in wb.Cookies)
                                    {
                                        if (d.Name == c.Name)

                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                        wb.Cookies.Add(c);
                                }
                            }
                        }
                        catch (Exception)
                        {

                        }

                    }
                    else
                    {
                        wb.Cookies = cookies;
                    }
                    if ((response.Headers != null) && (response.Headers.Any()))
                    {
                        if (wb.Headers==null)
                            wb.Headers=new NameValueCollection();
                        foreach (KeyValuePair<string, IEnumerable<string>> h in response.Headers)
                        {
                            foreach (string r in h.Value)
                            {
                                string val = r;
                                if (val.StartsWith("\"") && val.EndsWith("\""))
                                    val = val.Substring(1, val.Length - 2);
                                wb.Headers[h.Key] = val;
                            }
                        }
                    }
                    wb.StatusCode = response.StatusCode;
                    if ((wb.StatusCode == HttpStatusCode.Found) || (wb.StatusCode == HttpStatusCode.Moved) ||
                        (wb.StatusCode == HttpStatusCode.Redirect))
                    {
                        referer = url;
                        if (!redirect)
                            return wb;
                        wb.Dispose();
                        if (response.Headers.Location.ToString().StartsWith("/"))
                            url = u.Scheme + "://" + u.Host + response.Headers.Location;
                        else
                            url = response.Headers.Location.AbsoluteUri;
                        UriBuilder bld = new UriBuilder(url);
                        bld.Scheme = bas.Scheme;
                        url = bld.Uri.ToString();
                        cookies = wb.Cookies;
                        postData = null;
                    }
                } while ((wb.StatusCode == HttpStatusCode.Found) ||
                         (wb.StatusCode == HttpStatusCode.Moved) ||
                         (wb.StatusCode == HttpStatusCode.Redirect));
                return wb;
            }
            catch (Exception e)
            {
                wb?.Dispose();
                return null;
            }
        }
Exemplo n.º 2
0
        public static async Task <WebStream> CreateStream(string url, string postData, string encoding,
                                                          string postencoding         = "application/x-www-form-urlencoded", string uagent = null,
                                                          NameValueCollection headers = null, CookieCollection cookies                     = null, int timeout = 10000,
                                                          bool redirect = true, string referer = null, IWebProxy proxy = null)
        {
            WebStream wb = new WebStream();

            try
            {
                do
                {
                    Uri u = new Uri(url);
                    if (string.IsNullOrEmpty(encoding))
                    {
                        encoding = "UTF-8";
                    }
                    int t   = ServicePointManager.DefaultConnectionLimit;
                    Uri bas = new Uri(u.Scheme + "://" + u.Host);
                    HttpRequestMessage msg =
                        new HttpRequestMessage(string.IsNullOrEmpty(postData) ? HttpMethod.Get : HttpMethod.Post, u);
                    wb.DisposableObjects.Add(msg);
                    if (!string.IsNullOrEmpty(uagent))
                    {
                        msg.Headers.UserAgent.ParseAdd(uagent);
                    }
                    if (!string.IsNullOrEmpty(postData))
                    {
                        byte[] dta = Encoding.GetEncoding(encoding).GetBytes(postData);
                        msg.Content = new ByteArrayContent(dta, 0, dta.Length);
                        msg.Content.Headers.ContentType = new MediaTypeHeaderValue(postencoding);
                    }
                    if (headers != null)
                    {
                        PopulateHeaders(msg, headers);
                    }
                    if (referer != null)
                    {
                        msg.Headers.Referrer = new Uri(referer);
                    }
                    HttpClientHandler handler = new HttpClientHandler();
                    wb.DisposableObjects.Add(handler);
                    handler.AllowAutoRedirect      = false;
                    handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                    if (cookies != null && cookies.Count > 0)
                    {
                        handler.CookieContainer = new CookieContainer();
                        foreach (Cookie c in cookies)
                        {
                            if (string.IsNullOrEmpty(c.Domain))
                            {
                                c.Domain = bas.Host;
                            }

                            handler.CookieContainer.Add(c);
                        }
                    }
                    if (proxy != null)
                    {
                        handler.Proxy = proxy;
                    }
                    HttpClient http = new HttpClient(handler);
                    wb.DisposableObjects.Add(http);
                    http.Timeout = TimeSpan.FromMilliseconds(timeout);
                    HttpResponseMessage response = await http.SendAsync(msg);

                    wb.DisposableObjects.Add(response);
                    wb.DisposableObjects.Add(response.Content);
                    wb.baseStream = await response.Content.ReadAsStreamAsync();

                    wb.ContentType     = response.Content.Headers.ContentType.MediaType;
                    wb.ContentEncoding = response.Content.Headers.ContentEncoding.ToString();
                    wb.ContentLength   = response.Content.Headers.ContentLength ?? 0;
                    wb.Headers         = new NameValueCollection();
                    if (response.Headers.Contains("Set-Cookie"))
                    {
                        try
                        {
                            IEnumerable <string> sss = null;
                            response.Headers.TryGetValues("Set-Cookie", out sss);
                            wb.Cookies = new CookieCollection();
                            CookieCollection coll = null;
                            if (sss != null)
                            {
                                wb.Cookies = GetAllCookiesFromHeader(sss, bas.ToString());
                            }
                            if (cookies != null && cookies.Count > 0)
                            {
                                foreach (Cookie c in cookies)
                                {
                                    bool found = false;
                                    foreach (Cookie d in wb.Cookies)
                                    {
                                        if (d.Name == c.Name)

                                        {
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found)
                                    {
                                        wb.Cookies.Add(c);
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        wb.Cookies = cookies;
                    }
                    if ((response.Headers != null) && (response.Headers.Any()))
                    {
                        if (wb.Headers == null)
                        {
                            wb.Headers = new NameValueCollection();
                        }
                        foreach (KeyValuePair <string, IEnumerable <string> > h in response.Headers)
                        {
                            foreach (string r in h.Value)
                            {
                                string val = r;
                                if (val.StartsWith("\"") && val.EndsWith("\""))
                                {
                                    val = val.Substring(1, val.Length - 2);
                                }
                                wb.Headers[h.Key] = val;
                            }
                        }
                    }
                    wb.StatusCode = response.StatusCode;
                    if ((wb.StatusCode == HttpStatusCode.Found) || (wb.StatusCode == HttpStatusCode.Moved) ||
                        (wb.StatusCode == HttpStatusCode.Redirect))
                    {
                        referer = url;
                        if (!redirect)
                        {
                            return(wb);
                        }
                        wb.Dispose();
                        if (response.Headers.Location.ToString().StartsWith("/"))
                        {
                            url = u.Scheme + "://" + u.Host + response.Headers.Location;
                        }
                        else
                        {
                            url = response.Headers.Location.AbsoluteUri;
                        }
                        UriBuilder bld = new UriBuilder(url);
                        bld.Scheme = bas.Scheme;
                        url        = bld.Uri.ToString();
                        cookies    = wb.Cookies;
                        postData   = null;
                    }
                } while ((wb.StatusCode == HttpStatusCode.Found) ||
                         (wb.StatusCode == HttpStatusCode.Moved) ||
                         (wb.StatusCode == HttpStatusCode.Redirect));
                return(wb);
            }
            catch (Exception e)
            {
                wb?.Dispose();
                return(null);
            }
        }