0

I'm trying to submit a form on a web page programmatically. I'm practicing on the comment form of my Wordpress website: http://www.smortazavi.com/games/before-eternity/contact/

And here is the code based on this post:

private async void button_Click(object sender, RoutedEventArgs e)
{
    using (var client = new HttpClient())
    {
        var values = new Dictionary<string, string>
        {
           { "g16-name", "hello" },
           { "g16-email", "myemail@hotmail.com" },
            {"g16-comment", "comment" }
        };

        var content = new FormUrlEncodedContent(values);

        var response = await client.PostAsync("http://www.smortazavi.com/games/before-eternity/contact", content);

        var responseString = await response.Content.ReadAsStringAsync();
    }

    MessageBox.Show("Done!");   
}

When I run the code, the responseString contains the populated form inputs, but the form is not actually submitted.

Could you say what am I missing here?

Community
  • 1
  • 1
Siavash Mortazavi
  • 1,382
  • 1
  • 14
  • 18
  • The code seems to be fine. I'm using nearly the same in a project. I would try to turn off the form validation. Maybe the validation blocks the submission of the HttpClient filled input fields. – M. Altmann Nov 27 '15 at 07:08
  • Thank you @maltmann, this was just a test, the actual form I would like to submit is a time log system at work, which I don't have control over! Moreover, it requires the user to be logged into the system, so maybe it is not very possible to do it this way! :/ Thanks anyway ;-) – Siavash Mortazavi Nov 27 '15 at 13:36

1 Answers1

0

A quick peek at the HTML shows that the form contains 2 hidden fields:

<input type="hidden" name="contact-form-id" value="16">
<input type="hidden" name="action" value="grunion-contact-form">

Presumably those would need to get passed as well. Add the name/value pairs above to your values dictionary.

If that still doesn't work, pull up your browser debugger of choice (such as the Network tab in Chrome's DevTools), submit the form, and carefully inspect the request that was sent to make sure the URL is correct and you're not missing any other form fields. Your code looks good, so it's definitely details specific to that form that you're missing.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173