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, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var kvp in formValues) { var element = Assert.IsAssignableFrom <IHtmlInputElement>(form[kvp.Key]); element.Value = kvp.Value; } var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), 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)); }
/// <summary> /// Submits a HTML form to the server /// </summary> /// <param name="client">HTTP client to use for submitting the form.</param> /// <param name="form">HTML Form to submit.</param> /// <param name="submitButton">Submit button to use for submitting the form.</param> /// <returns>Returns the HTTP response received after submitting the form.</returns> public static Task <HttpResponseMessage> SubmitFormAsync(this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton) { if (form == null) { throw new ArgumentNullException(nameof(form)); } if (submitButton == null) { throw new ArgumentNullException(nameof(submitButton)); } var formSubmission = form.GetSubmission(submitButton); var targetUri = (Uri)formSubmission.Target; if (submitButton.HasAttribute("formaction")) { var formAction = submitButton.GetAttribute("formaction"); targetUri = new Uri(formAction, UriKind.Relative); } var submitFormRequest = new HttpRequestMessage(new HttpMethod(formSubmission.Method.ToString()), targetUri) { Content = new StreamContent(formSubmission.Body) }; foreach (var header in formSubmission.Headers) { submitFormRequest.Headers.TryAddWithoutValidation(header.Key, header.Value); submitFormRequest.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); } return(client.SendAsync(submitFormRequest)); }
private static string GetAntiForgeryTokenFromForm(IHtmlFormElement form) { using (var reader = new StreamReader(form.GetSubmission().Body)) { var submitBody = reader.ReadToEnd(); var tokenMatch = TOKEN_REGEX.Match(submitBody); var token = tokenMatch.Success ? tokenMatch.Groups[1].Captures[0].Value : null; return(token); } }
public static Task <HttpResponseMessage> SendAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { foreach (var kvp in formValues) { switch (form[kvp.Key]) { case IHtmlInputElement input: input.Value = kvp.Value; if (bool.TryParse(kvp.Value, out var isChecked)) { input.IsChecked = isChecked; } break; case IHtmlSelectElement select: select.Value = kvp.Value; break; default: throw new Exception($"Unknown form element: '{kvp.Key}'"); } } var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), 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, 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)); }
public static Task <HttpResponseMessage> SendFormAsync( this HttpClient client, IHtmlFormElement form, IHtmlElement submitButton, IEnumerable <KeyValuePair <string, string> > formValues) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (form is null) { throw new ArgumentNullException(nameof(form)); } if (submitButton is null) { throw new ArgumentNullException(nameof(submitButton)); } if (formValues is null) { throw new ArgumentNullException(nameof(formValues)); } foreach (var kvp in formValues) { var element = form[kvp.Key]; switch (element) { case IHtmlInputElement input: input.Value = kvp.Value; break; case IHtmlSelectElement select: select.Value = kvp.Value; break; } } Assert.True(form.CheckValidity(), "Form contains invalid data"); var submit = form.GetSubmission(submitButton); var target = (Uri)submit.Target; if (submitButton.HasAttribute("formaction")) { var formaction = submitButton.GetAttribute("formaction"); target = new Uri(formaction, UriKind.Relative); } #pragma warning disable CA2000 // Dispose objects before losing scope var submision = new HttpRequestMessage(new HttpMethod(submit.Method.ToString()), target) { Content = new StreamContent(submit.Body) }; #pragma warning restore CA2000 // Dispose objects before losing scope 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)); }