Esempio n. 1
0
        private void LoadTypes(String html, ResponseHttp responseHttp)
        {
            if (TypeComponent == Enums.TypeComponent.None)
            {
                return;
            }

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(html);

            if ((TypeComponent & Enums.TypeComponent.ComboBox) == Enums.TypeComponent.ComboBox)
            {
                responseHttp.Components.ComboBoxes = doc.ParseCombo();
            }

            if ((TypeComponent & Enums.TypeComponent.LinkButton) == Enums.TypeComponent.LinkButton)
            {
                responseHttp.Components.LinkButtons = doc.ParseLink();
            }

            if ((TypeComponent & Enums.TypeComponent.DataGrid) == Enums.TypeComponent.DataGrid)
            {
                responseHttp.Components.Grids = doc.ParseGrid();
            }

            if ((TypeComponent & Enums.TypeComponent.InputCheckbox) == Enums.TypeComponent.InputCheckbox)
            {
                responseHttp.Components.InputCheckBoxes = doc.ParseCheckbox();
            }

            if ((TypeComponent & Enums.TypeComponent.Image) == Enums.TypeComponent.Image)
            {
                responseHttp.Components.Images = doc.ParseImage();
            }

            if ((TypeComponent & Enums.TypeComponent.InputText) == Enums.TypeComponent.InputText)
            {
                responseHttp.Components.InputTexts = doc.ParseInputText();
            }

            if ((TypeComponent & Enums.TypeComponent.InputHidden) == Enums.TypeComponent.InputHidden)
            {
                responseHttp.Components.InputHidden = doc.ParseInputHiddenText();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adiciona uma coleção de cookies na requisição
        /// </summary>
        /// <param name="collection">coleção de cookies</param>
        /// <param name="responseHttp">requisição</param>
        public void AddCookies(CookieCollection collection, ResponseHttp responseHttp)
        {
            if (collection == null)
            {
                return;
            }
            foreach (Cookie cookie in collection)
            {
                if (this.AllCookies[cookie.Name] != null && this.AllCookies[cookie.Name].Domain == cookie.Domain)
                {
                    this.AllCookies[cookie.Name].Value = cookie.Value;
                }
                else
                {
                    this.AllCookies.Add(cookie);
                }

                responseHttp.CookiesAdded.Add(cookie);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Adiciona os cookies do resposta na requisição
        /// </summary>
        /// <param name="httpRes">resposta</param>
        /// <param name="responseHttp">requisição</param>
        protected void AddInternalCookie(HttpWebResponse httpRes, ResponseHttp responseHttp)
        {
            if (httpRes.Headers["Set-Cookie"] == null)
            {
                return;
            }

            string str1  = httpRes.Headers["Set-Cookie"].ToString().Trim().Split(';')[0];
            string name  = str1.Split('=')[0];
            string str2  = str1.Split('=')[1];
            string empty = string.Empty;

            string str3;

            if (httpRes.Headers["Set-Cookie"].ToString().Trim().Contains(";"))
            {
                str3 = httpRes.Headers["Set-Cookie"].ToString().Trim().Split(';')[0].Split('=')[1];
            }
            else
            {
                str3 = httpRes.Headers["Set-Cookie"].ToString().Trim();
            }

            string domain = httpRes.ResponseUri.Host;

            if (domain.StartsWith("www."))
            {
                domain = domain.Replace("www.", "");
            }

            if (this.AllCookies[name] == null)
            {
                var cookie = new Cookie(name, str3, empty, domain);
                this.AllCookies.Add(cookie);
                responseHttp.CookiesAdded.Add(cookie);
            }
        }
Esempio n. 4
0
        internal ResponseHttp LoadPage(string url)
        {
            responseHttp = new ResponseHttp();
            this.Url     = url;
            lock (SyncRoot)
            {
                try
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                    httpWebRequest.UserAgent       = UserAgent;
                    httpWebRequest.PreAuthenticate = PreAuthenticate;
                    httpWebRequest.Accept          = Accept;
                    httpWebRequest.Timeout         = TimeoutRequest;
                    httpWebRequest.KeepAlive       = KeepAlive;

                    if (this.Parameters != null && this.Parameters.Length > 0)
                    {
                        httpWebRequest.Method = "POST";
                    }
                    else
                    {
                        httpWebRequest.Method = "GET";
                    }

                    httpWebRequest.Headers["Accept-Encoding"] = AcceptEncoding;
                    httpWebRequest.Headers["Accept-Language"] = AcceptLanguage;
                    httpWebRequest.Headers["UA-CPU"]          = UACPU;
                    httpWebRequest.Headers["Cache-Control"]   = CacheControl;

                    if (!string.IsNullOrEmpty(RequestedWith))
                    {
                        httpWebRequest.Headers["x-requested-with"] = RequestedWith;
                    }

                    httpWebRequest.ContentLength     = 0L;
                    httpWebRequest.AllowAutoRedirect = AutoRedirect;

                    if (Headers != null)
                    {
                        foreach (KeyValuePair <string, string> header in Headers)
                        {
                            httpWebRequest.Headers.Add(header.Key, header.Value);
                        }
                    }

                    if (Referer != null)
                    {
                        httpWebRequest.Referer = Referer;
                    }
                    httpWebRequest.CookieContainer = new CookieContainer();

                    if (AllCookies.Count > 0)
                    {
                        foreach (Cookie todosCookie in AllCookies)
                        {
                            httpWebRequest.CookieContainer.Add(todosCookie);
                        }
                    }

                    if (Parameters != null && Parameters.Trim().Length > 0)
                    {
                        byte[] bytes = EncodingPage.GetBytes(Parameters);
                        httpWebRequest.Method        = "POST";
                        httpWebRequest.ContentType   = ContentType;
                        httpWebRequest.ContentLength = bytes.Length;
                        using (Stream requestStream = httpWebRequest.GetRequestStream())
                        {
                            requestStream.Write(bytes, 0, bytes.Length);
                            requestStream.Close();
                        }
                    }

                    string html;
                    string responseHeader;
                    using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
                    {
                        responseHttp.StatusCode = response.StatusCode;

                        if (response.ContentType.StartsWith("image"))
                        {
                            using (Stream responseStream = response.GetResponseStream())
                                html = string.Format("data:{0};base64,{1}", response.ContentType, Convert.ToBase64String(responseStream.ReadAllBytes()));
                        }
                        else
                        {
                            using (Stream responseStream = response.GetResponseStream())
                            {
                                Stream stream = null;
                                try
                                {
                                    stream = string.IsNullOrEmpty(response.ContentEncoding) || !response.ContentEncoding.ToLower().Contains("gzip") ? (string.IsNullOrEmpty(response.ContentEncoding) || !response.ContentEncoding.ToLower().Contains("deflate") ? responseStream : new DeflateStream(responseStream, CompressionMode.Decompress)) : new GZipStream(responseStream, CompressionMode.Decompress);
                                    using (StreamReader streamReader = new StreamReader(stream, DefaultEncoding))
                                    {
                                        html = streamReader.ReadToEnd();
                                        streamReader.Close();
                                    }
                                    responseStream.Close();
                                }
                                finally
                                {
                                    if (stream != null)
                                    {
                                        stream.Dispose();
                                    }
                                }
                            }
                        }
                        AddCookies(response.Cookies, responseHttp);
                        AddInternalCookie(response, responseHttp);
                        responseHeader            = response.GetResponseHeader("Location");
                        responseHttp.UrlLocation  = responseHeader;
                        responseHttp.Method       = response.Method;
                        responseHttp.Server       = response.Server;
                        responseHttp.HeadersAdded = this.Headers;
                        response.Close();
                    }

                    if (MaxRedirect > 0)
                    {
                        if (!string.IsNullOrEmpty(responseHeader) && Uri.IsWellFormedUriString(responseHeader, UriKind.Absolute))
                        {
                            MaxRedirect--;
                            this.Referer = url;
                            return(LoadPage(responseHeader));
                        }
                    }
                    responseHttp.HtmlPage = html;

                    LoadTypes(html, responseHttp);

                    OnLoad?.Invoke(this, new RequestHttpEventArgs(html, responseHttp));

                    return(responseHttp);
                }
                catch (WebException ex)
                {
                    responseHttp.StatusCode = HttpStatusCode.InternalServerError;
                }
                //catch (Exception ex)
                //{
                //    return null;
                //}

                return(responseHttp);
            }
        }
 public RequestHttpEventArgs(string page, ResponseHttp responseHttp)
 {
     this.HtmlPage     = page;
     this.ResponseHttp = responseHttp;
 }