コード例 #1
0
        /// <summary>
        /// Adiciona os cookies do resposta na requisição
        /// </summary>
        /// <param name="httpRes">resposta</param>
        /// <param name="responseHttp">requisição</param>
        protected void AddInternalHeaders(HttpWebResponse httpRes, ResponseHttp responseHttp)
        {
            foreach (var item in httpRes.Headers)
            {
                string nameHeader = item.ToString();
                if (nameHeader == "Set-Cookie" || nameHeader == "Connection" || nameHeader == "Content-Length" ||
                    nameHeader == "Date" || nameHeader == "Transfer-Encoding")
                {
                    continue;
                }

                AddHeader(nameHeader, httpRes.Headers[nameHeader]);
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
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);
            }
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        internal async Task <ResponseHttp> LoadPageAsync(string url)
        {
            responseHttp = new ResponseHttp();
            this.Url     = url;

            try
            {
                HttpWebRequest httpWebRequest = CreateRequest();

                string html;
                string responseHeader;
                using (HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync())
                {
                    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
                    {
                        html = GetResponseHtml(response);
                    }

                    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;
                }

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

                LoadTypes(html, responseHttp);

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

                return(responseHttp);
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        responseHttp.HtmlPage = reader.ReadToEnd();
                    }
                }
                responseHttp.StatusCode = HttpStatusCode.InternalServerError;
            }
            //catch (Exception ex)
            //{
            //    return null;
            //}

            return(responseHttp);
        }
コード例 #6
0
 public RequestHttpEventArgs(string page, ResponseHttp responseHttp)
 {
     this.HtmlPage     = page;
     this.ResponseHttp = responseHttp;
 }
コード例 #7
0
        internal async Task <ResponseHttp> LoadPageAsync(string url)
        {
            responseHttp = new ResponseHttp();
            this.Url     = url;

            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)await httpWebRequest.GetResponseAsync())
                {
                    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
                    {
                        html = GetResponseHtml(response);
                    }

                    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;
                }

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

                LoadTypes(html, responseHttp);

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

                return(responseHttp);
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        responseHttp.HtmlPage = reader.ReadToEnd();
                    }
                }
                responseHttp.StatusCode = HttpStatusCode.InternalServerError;
            }
            //catch (Exception ex)
            //{
            //    return null;
            //}

            return(responseHttp);
        }