0

It is a WindowsForm application. I used C# code to get the HTML string from a website successfully.

Inside the HTML code, the form looks like this:

<form id="editform" name="editform" method="post" action="/blablabla/index.php?title=title1&action=submit" enctype="multipart/form-data">    
    <textarea tabindex='1' accesskey="," name="Textbox1" id="Textbox1" rows='25' cols='80' >
    item1;
    item2;
    ....

    </textarea>
    <input id="wpSave" name="wpSave" type="submit" tabindex="5" value="Save page" accesskey="s" title="Save your changes" />
</form>

I tried to use

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
...

these ways to get the text inside "textarea" successfully, but I am stuck on submitting the edited text to the original website. To clarify, I want to edit a textarea inside an HTML form of a website using my WinForm Application, and submit it.

What is the best way to use C# to modify the text inside "textarea", and submit the changed text to the original website? Thank you.

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
alvinchen
  • 33
  • 6

1 Answers1

0

Modified from https://stackoverflow.com/a/19983672/1178781:

public static string postTextAre(string text, string postUrl) {
    HttpClient httpClient = new HttpClient();
    MultipartFormDataContent form = new MultipartFormDataContent();

    form.Add(new StringContent(text), "Textbox1");
    form.Add(new StringContent("Save page"), "wpSave");
    HttpResponseMessage response = await httpClient.PostAsync(postUrl, form);

    response.EnsureSuccessStatusCode();
    httpClient.Dispose();
    return response.Content.ReadAsStringAsync().Result;
}

If you do not want to use HttpClient, you can re-used the anser for https://stackoverflow.com/a/20000831/1178781 and just tweak the code so it doesn't upload a picture - just submit the value for Textbox1.

Community
  • 1
  • 1
justderb
  • 2,675
  • 1
  • 24
  • 38