【问题标题】:Submit html form with textarea使用 textarea 提交 html 表单
【发布时间】:2016-11-11 19:55:46
【问题描述】:

它是一个 WindowsForm 应用程序。我使用 C# 代码成功地从网站获取 HTML 字符串。

在 HTML 代码中,表单如下所示:

<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>

我尝试使用

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

这些方法可以成功获取“textarea”中的文本,但我坚持将编辑后的文本提交到原始网站。澄清一下,我想使用我的 WinForm 应用程序编辑网站 HTML 表单中的文本区域,然后提交。

使用C#修改“textarea”内的文本,并将修改后的文本提交到原网站的最佳方法是什么?谢谢。

【问题讨论】:

  • 你打算用编辑后的 ​​HTML 做什么?什么是提交到原始网站?您想更改网站的内容,还是只是尝试使用 C# 填写表单并将其发布到 blablabla/index?
  • 你究竟为什么要标记 PHP?
  • @Hespen 问题已更新。谢谢。
  • 那么,您想模仿通过 C# 应用程序提交表单吗?使用此应用程序,可以发送最初通过 textarea 发送的任何“文本”?
  • @justderb 没错!

标签: c# html winforms web


【解决方案1】:

修改自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;
}

如果您不想使用HttpClient,您可以重新使用https://stackoverflow.com/a/20000831/1178781 的分析器并调整代码使其不上传图片 - 只需提交Textbox1 的值。

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2019-12-26
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2013-04-15
    • 2012-01-10
    相关资源
    最近更新 更多