0

By using WebClient in C#, I can get the source of a page. Imagine that this HTML source always contains a form with a given name like this:

<form name="myform" action="page.php">
    <input type="text" name="mytext" value="default text">
    <textarea name="mytextarea">content</textarea>
    <input type="password" name="mypass" value="12345">
    <input type="hidden" name="myhiddenfield" value="code">
    <input type="checkbox" name="mychb1" value="true" checked>
    <input type="checkbox" name="mychb2" value="true">
    <input type="radio" name="myradio" value="radio2" checked>
    <input type="radio" name="myradio" value="radio1">
    <select name="mychoice">
      <option value="cherry">cherry</option>
      <option value="orange">orange</option>
      <option selected="" value="apple">apple</option>
    </select>
    <input type="submit" value="Submit">
</form>

Now I want to make POST data according to these values like this:

mytext=default text&mytextarea=content&mypass=12345&myhiddenfield=code&mychb=true&myradio=radio2&mychoice=apple

Note that mychb2 is not included because is not checked and also the names might be vary in each page source and there might be more or less than elements in the forms.

How can I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
asDca21
  • 161
  • 1
  • 2
  • 9
  • Possible duplicate of [How to submit http form using C#](http://stackoverflow.com/questions/1273998/how-to-submit-http-form-using-c-sharp) – Oceans Dec 28 '15 at 12:11
  • @Oceans In the post you point, the names, count of elements and types of them are same. I can handle forms with fixed elements but not for different forms and this is my problem. – asDca21 Dec 28 '15 at 12:12
  • The obvious answer would be to simply give null values for the missing elements. -- You need to have some kind of model to work with, you can make this more dynamic but you'll have to figure something to build on. Using generic methods and and parameters will probably be the solution but ultimately you'll also need to use strong typed properties. There is no way to achieve infinite variations using a single method unless you can predict these changes. – Oceans Dec 28 '15 at 12:22

0 Answers0