Ejemplo n.º 1
0
 /// <summary>
 /// Submit form. /*//NOTICE: parameters with null value will not submitted.*/
 /// </summary>
 /// <param name="form">parsed form</param>
 /// <param name="send_cookies">defines whether cookies should be sent</param>
 /// <returns>true if received success response</returns>
 public bool SubmitForm(HtmlForm form, bool send_cookies)
 {
     return Do(new HttpRequest(form), send_cookies);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Submit form with sending cookies. /*//NOTICE: parameters with null value will not submitted.*/
 /// </summary>
 /// <param name="form">parsed form</param>
 /// <param name="send_cookies">defines whether cookies should be sent</param>
 /// <returns>true if received success response</returns>
 public bool SubmitForm(HtmlForm form)
 {
     return Do(new HttpRequest(form), true);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Parse input html page for form containing specified fragment and create HtmlForm from it
        /// </summary>
        /// <param name="page">html page</param>
        /// <param name="url">url of the input html page</param>
        /// <param name="fragment">part of html code that contained by the form. Can be null.</param>
        /// <returns>object presenting found and parsed form</returns>
        public HtmlForm ParseForm(string page, string url, string fragment)
        {
            HtmlForm hf = null;

            try
            {
                Match m = Regex.Match(page, @"<form\s.*?</form.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    if (fragment == null || m.Result("$0").Contains(fragment))
                    {
                        hf = new HtmlForm();
                        break;
                    }
                    m = m.NextMatch();
                }

                if (hf == null)
                    return null;

                string form = m.Result("$0");

                m = Regex.Match(form, @"<form[^>]*?\smethod=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (m.Success && m.Result("$2").ToUpper() == HttpRequest.RequestMethod.POST.ToString())
                {
                    hf.Method = HttpRequest.RequestMethod.POST;
                }

                m = Regex.Match(form, @"<form[^>]*?\saction=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    Uri ubase = new Uri(url, UriKind.Absolute);
                    string link = HttpUtility.HtmlDecode(m.Result("$2"));
                    Uri ulink = new Uri(ubase, link);
                    hf.Url = ulink.AbsoluteUri.Trim();
                }
                else
                {
                    hf.Url = url;
                }

                m = Regex.Match(form, @"<input\s[^>]+?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    string input_element = m.Result("$0");
                    Match m2 = Regex.Match(input_element, @"\sname=(['""]?)(.+?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                    {
                        m = m.NextMatch();
                        continue;
                    }

                    string name = m2.Result("$2");

                    string type = "";
                    m2 = Regex.Match(input_element, @"\stype=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (m2.Success)
                    {
                        type = m2.Result("$2");
                    }
                    type = type.ToUpper().Trim();

                    bool ignore_value = false;
                    if (type == "CHECKBOX")
                    {
                        m2 = Regex.Match(input_element, @"\s(['""]?)checked\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                        if (!m2.Success)
                            ignore_value = true;
                    }

                    string value = null;
                    m2 = Regex.Match(input_element, @"\svalue=(['""]?)(.*?)\1", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                        m2 = Regex.Match(input_element, @"\svalue(=)(.*?)[\s>]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (m2.Success)
                    {
                        if (!ignore_value)
                            value = m2.Result("$2");
                    }

                    if (type == HtmlForm.ParameterType.HIDDEN.ToString())
                        hf.Create(name, HtmlForm.ParameterType.HIDDEN, value);
                    else if (type == "" || type == HtmlForm.ParameterType.TEXT.ToString())
                        hf.Create(name, HtmlForm.ParameterType.TEXT, value);
                    else if (type == HtmlForm.ParameterType.CHECKBOX.ToString())
                        hf.Create(name, HtmlForm.ParameterType.CHECKBOX, value);
                    else if (type == HtmlForm.ParameterType.RADIO.ToString())
                        hf.Create(name, HtmlForm.ParameterType.RADIO, value);
                    else if (type == HtmlForm.ParameterType.SUBMIT.ToString())
                        hf.Create(name, HtmlForm.ParameterType.SUBMIT, value);
                    else if (type == HtmlForm.ParameterType.RESET.ToString())
                        hf.Create(name, HtmlForm.ParameterType.RESET, value);
                    else if (type == HtmlForm.ParameterType.BUTTON.ToString())
                        hf.Create(name, HtmlForm.ParameterType.BUTTON, value);
                    else if (type == HtmlForm.ParameterType.PASSWORD.ToString())
                        hf.Create(name, HtmlForm.ParameterType.PASSWORD, value);
                    else if (type == HtmlForm.ParameterType.FILE.ToString())
                        hf.Create(name, HtmlForm.ParameterType.FILE, value);
                    else
                        hf.Create(name, HtmlForm.ParameterType.OTHER, value);

                    m = m.NextMatch();
                }

                m = Regex.Match(form, @"<textarea(\s[^>]*?)>(.*?)</textarea", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    Match m2 = Regex.Match(m.Result("$1"), @"\sname=['""]?(.*?)['""\s>]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                        continue;

                    string name = m2.Result("$1");
                    string value = m.Result("$2");

                    hf.Create(name, HtmlForm.ParameterType.TEXTAREA, value);

                    m = m.NextMatch();
                }

                m = Regex.Match(form, @"<select(\s[^>]*?>)(.*?)</select", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    Match m2 = Regex.Match(m.Result("$1"), @"[\s""']name=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                        continue;

                    string name = m2.Result("$2");
                    hf.Create(name, HtmlForm.ParameterType.SELECT);

                    m2 = Regex.Match(m.Result("$2"), @"<OPTION(.*?>)(.*?)</OPTION", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    while (m2.Success)
                    {
                        if (Regex.IsMatch(m2.Result("$1"), @" selected", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase))
                        {
                            string value = null;
                            Match m3 = Regex.Match(m2.Result("$1"), @"\svalue=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                            if (m3.Success)
                                value = m3.Result("$2");
                            else
                                value = m2.Result("$2");
                            hf.Add(name, value);
                        }
                        m2 = m2.NextMatch();
                    }

                    m = m.NextMatch();
                }

                return hf;
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception e)
            {
                LogMessage.Error(e);
            }
            return null;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Downloads web page by POST. 
 /// </summary>
 /// <param name="form">form with parameters. /*//NOTICE: parameters with null value will not be submitted.*/</param>
 /// <param name="send_cookies">defines if cookies should be sent</param>
 /// <returns>reqest status</returns>
 public bool PostPage(HtmlForm form, bool send_cookies = true)
 {
     return Do(new HttpRequest(form, new Dictionary<string, string>() { { "Accept", Properties.Web.Default.TextModeHttpRequestAcceptHeader } }), send_cookies);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Parse input html page for form containing specified fragment and create HtmlForm from it
        /// </summary>
        /// <param name="page">html page</param>
        /// <param name="url">url of the input html page</param>
        /// <param name="fragment">part of html code that contained by the form. Can be null.</param>
        /// <returns>object presenting found and parsed form</returns>
        public HtmlForm ParseForm(string page, string url, string fragment)
        {
            HtmlForm hf = null;

            try
            {
                Match m = Regex.Match(page, @"<form\s.*?</form.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    if (fragment == null || m.Result("$0").Contains(fragment))
                    {
                        hf = new HtmlForm();
                        break;
                    }
                    m = m.NextMatch();
                }

                if (hf == null)
                {
                    return(null);
                }

                string form = m.Result("$0");

                m = Regex.Match(form, @"<form[^>]*?\smethod=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (m.Success && m.Result("$2").ToUpper() == HttpRequest.RequestMethod.POST.ToString())
                {
                    hf.Method = HttpRequest.RequestMethod.POST;
                }

                m = Regex.Match(form, @"<form[^>]*?\saction=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    Uri    ubase = new Uri(url, UriKind.Absolute);
                    string link  = HttpUtility.HtmlDecode(m.Result("$2"));
                    Uri    ulink = new Uri(ubase, link);
                    hf.Url = ulink.AbsoluteUri.Trim();
                }
                else
                {
                    hf.Url = url;
                }

                m = Regex.Match(form, @"<input\s[^>]+?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    string input_element = m.Result("$0");
                    Match  m2            = Regex.Match(input_element, @"\sname=(['""]?)(.+?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                    {
                        m = m.NextMatch();
                        continue;
                    }

                    string name = m2.Result("$2");

                    string type = "";
                    m2 = Regex.Match(input_element, @"\stype=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (m2.Success)
                    {
                        type = m2.Result("$2");
                    }
                    type = type.ToUpper().Trim();

                    bool ignore_value = false;
                    if (type == "CHECKBOX")
                    {
                        m2 = Regex.Match(input_element, @"\s(['""]?)checked\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                        if (!m2.Success)
                        {
                            ignore_value = true;
                        }
                    }

                    string value = null;
                    m2 = Regex.Match(input_element, @"\svalue=(['""]?)(.*?)\1", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                    {
                        m2 = Regex.Match(input_element, @"\svalue(=)(.*?)[\s>]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    }
                    if (m2.Success)
                    {
                        if (!ignore_value)
                        {
                            value = m2.Result("$2");
                        }
                    }

                    if (type == HtmlForm.ParameterType.HIDDEN.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.HIDDEN, value);
                    }
                    else if (type == "" || type == HtmlForm.ParameterType.TEXT.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.TEXT, value);
                    }
                    else if (type == HtmlForm.ParameterType.CHECKBOX.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.CHECKBOX, value);
                    }
                    else if (type == HtmlForm.ParameterType.RADIO.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.RADIO, value);
                    }
                    else if (type == HtmlForm.ParameterType.SUBMIT.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.SUBMIT, value);
                    }
                    else if (type == HtmlForm.ParameterType.RESET.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.RESET, value);
                    }
                    else if (type == HtmlForm.ParameterType.BUTTON.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.BUTTON, value);
                    }
                    else if (type == HtmlForm.ParameterType.PASSWORD.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.PASSWORD, value);
                    }
                    else if (type == HtmlForm.ParameterType.FILE.ToString())
                    {
                        hf.Create(name, HtmlForm.ParameterType.FILE, value);
                    }
                    else
                    {
                        hf.Create(name, HtmlForm.ParameterType.OTHER, value);
                    }

                    m = m.NextMatch();
                }

                m = Regex.Match(form, @"<textarea(\s[^>]*?)>(.*?)</textarea", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    Match m2 = Regex.Match(m.Result("$1"), @"\sname=['""]?(.*?)['""\s>]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                    {
                        continue;
                    }

                    string name  = m2.Result("$1");
                    string value = m.Result("$2");

                    hf.Create(name, HtmlForm.ParameterType.TEXTAREA, value);

                    m = m.NextMatch();
                }

                m = Regex.Match(form, @"<select(\s[^>]*?>)(.*?)</select", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                while (m.Success)
                {
                    Match m2 = Regex.Match(m.Result("$1"), @"[\s""']name=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    if (!m2.Success)
                    {
                        continue;
                    }

                    string name = m2.Result("$2");
                    hf.Create(name, HtmlForm.ParameterType.SELECT);

                    m2 = Regex.Match(m.Result("$2"), @"<OPTION(.*?>)(.*?)</OPTION", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    while (m2.Success)
                    {
                        if (Regex.IsMatch(m2.Result("$1"), @" selected", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase))
                        {
                            string value = null;
                            Match  m3    = Regex.Match(m2.Result("$1"), @"\svalue=(['""]?)(.*?)\1.*?>", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
                            if (m3.Success)
                            {
                                value = m3.Result("$2");
                            }
                            else
                            {
                                value = m2.Result("$2");
                            }
                            hf.Add(name, value);
                        }
                        m2 = m2.NextMatch();
                    }

                    m = m.NextMatch();
                }

                return(hf);
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception e)
            {
                LogMessage.Error(e);
            }
            return(null);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Submit form with sending cookies. /*//NOTICE: parameters with null value will not submitted.*/
 /// </summary>
 /// <param name="form">parsed form</param>
 /// <param name="send_cookies">defines whether cookies should be sent</param>
 /// <returns>true if received success response</returns>
 public bool SubmitForm(HtmlForm form)
 {
     return(Do(new HttpRequest(form), true));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Submit form. /*//NOTICE: parameters with null value will not submitted.*/
 /// </summary>
 /// <param name="form">parsed form</param>
 /// <param name="send_cookies">defines whether cookies should be sent</param>
 /// <returns>true if received success response</returns>
 public bool SubmitForm(HtmlForm form, bool send_cookies)
 {
     return(Do(new HttpRequest(form), send_cookies));
 }