public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IEnumerable <KeyValuePair <string, string> > formValues) { Assert.That(form.QuerySelectorAll("[type=submit]") != null); var submitElement = form.QuerySelectorAll("[type=submit]"); Assert.IsInstanceOf <IHtmlElement>(submitElement); var submitButton = submitElement as IHtmlElement; return(client.SendAsync(form, submitButton, formValues)); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var kvp in formValues) { var element = Assert.IsAssignableFrom <IHtmlInputElement>(form[kvp.Key]); element.Value = kvp.Value; } var submitElement = Assert.Single(form.QuerySelectorAll("[type=submit]")); var submitButton = Assert.IsAssignableFrom <IHtmlElement>(submitElement); var submit = form.GetSubmission(submitButton); var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), submit.Target) { Content = new StreamContent(submit.Body) }; foreach (var header in submit.Headers) { submision.Headers.TryAddWithoutValidation(header.Key, header.Value); submision.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submision)); }
public static Task <HttpResponseMessage> SendAsync(this HttpClient client, IHtmlFormElement form, IEnumerable <KeyValuePair <string, string> > formValues) { var submitElement = form.QuerySelectorAll("[type=submit]").Single(); var submitButton = (IHtmlElement)submitElement; return(client.SendAsync(form, submitButton, formValues)); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, [NotNull] IHtmlFormElement form, [NotNull] IEnumerable <KeyValuePair <string, string> > formValues) { var submitElement = Assert.Single(form.QuerySelectorAll("[type=submit]")); var submitButton = Assert.IsAssignableFrom <IHtmlElement>(submitElement); return(client.SendAsync(form, submitButton, formValues)); }
private static List <KeyValuePair <string, string> > GetFormInputs(IHtmlFormElement form) { var pairs = new List <KeyValuePair <string, string> >(); var inputElements = form.QuerySelectorAll("input"); foreach (IHtmlInputElement element in inputElements) { if (element.Name != null && element.Name != string.Empty) { if (element.Type.Equals("checkbox", StringComparison.OrdinalIgnoreCase) && !element.IsChecked) { continue; } if (element.Type.Equals("submit", StringComparison.OrdinalIgnoreCase)) { continue; } pairs.Add(new KeyValuePair <string, string>(element.Name, element.Value)); } } var selectElements = form.QuerySelectorAll("select"); foreach (IHtmlSelectElement element in selectElements) { if (element.SelectedIndex > -1) { var optionSelected = element[element.SelectedIndex]; pairs.Add(new KeyValuePair <string, string>(element.Name, optionSelected.Value)); } } return(pairs); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IEnumerable <KeyValuePair <string, string> > formValues) { if (form is null) { throw new ArgumentNullException(nameof(form)); } var submitElement = Assert.Single(form.QuerySelectorAll("[type=submit]")); var submitButton = Assert.IsAssignableFrom <IHtmlElement>(submitElement); return(client.SendFormAsync(form, submitButton, formValues)); }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var formValue in formValues) { if (form[formValue.Key] is IHtmlInputElement elementAsInput) { elementAsInput.Value = formValue.Value; } else if (form[formValue.Key] is IHtmlSelectElement) { var matchingSelect = form.QuerySelectorAll("select") .OfType <IHtmlSelectElement>() .FirstOrDefault(s => s.Children.OfType <IHtmlOptionElement>().Any(o => o.Value == formValue.Value)); if (matchingSelect != null) { matchingSelect.Value = formValue.Value; } } } var documentRequest = form.GetSubmission(submitButton); var requestUri = submitButton.HasAttribute("formaction") ? new Uri(submitButton.GetAttribute("formaction"), UriKind.Relative) : documentRequest.Target; var request = new HttpRequestMessage(new HttpMethod(documentRequest.Method.ToString()), requestUri) { Content = new StreamContent(documentRequest.Body) }; foreach (var header in documentRequest.Headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(request)); }
/// <summary> /// Finds a single submit button inside a HTML form. /// </summary> /// <param name="form">Form to look for the submit button.</param> /// <param name="identifier">ID of the element to locate.</param> /// <returns>Returns the located submit button.</returns> public static IHtmlElement SubmitButton(this IHtmlFormElement form, string identifier) { return((IHtmlElement)form .QuerySelectorAll("button[type='submit']") .FirstOrDefault(x => x.Id == identifier)); }