An extension for CsQuery to format a request body from a form element.
This extension simply adds the following extension methods to IHTMLFormElement
.
IEnumerable<NameValueType> GetNameValueTypes(this IHTMLFormElement form, IDomElement submitter)
- This allows you to get the data set with a specific submitter node (e.g. the button pressed).
IEnumerable<NameValueType> GetNameValueTypes(this IHTMLFormElement form, bool implicitSubmission)
- This allows you to get the data set using implicit submission if you pass
true
or no submitter at all if you passfalse
.
Here's a bit of code that downloads Reddit's front page (CQ.CreateFromUrl
) and interprets the DOM to get the names and values that would be POSTed on log in.
// get a CQ context
CQ document = CQ.CreateFromUrl("http://www.reddit.com");
// fill in some form fields
document["#login_login-main input[name=user]"].Val("foo");
document["#login_login-main input[name=passwd]"].Val("bar");
// get the containing form
IHTMLFormElement loginForm = document["#login_login-main"].OfType<IHTMLFormElement>().First();
// get the data representing the form, using implicit submission
IEnumerable<NameValueType> nameValueTypes = loginForm.GetNameValueTypes(true);
// output the results
foreach (var nameValueType in nameValueTypes)
{
Console.WriteLine("{0} = {1}", nameValueType.Name, nameValueType.Value);
}
The output should look something like this:
op = login-main
user = foo
passwd = bar
- CsQuery
- .NET 4.0 or newer
dirname
attributes.- file uploads (
<input type=file>
). - correct handling of
<textarea>
values. <keygen>
elements.
- Generating
HttpContent
from anIHTMLFormElement
. - Generating
HttpRequestMessage
from anIHTMLFormElement
and a URL.
The code is well tested, 100% code coverage on the CsQuery.FormFields
assembly.