将POST请求发送到具有多个表单的页面

时间:2011-11-29 21:24:29

标签: c# forms post

我发现this great class用于向网页发送POST请求。现在我在单个表单的测试页面上尝试了它并且它可以工作。但是现在我想对一个页面发出POST请求,该页面在同一页面上有多个表单。是否可以选择特定的表单,如果它是用名称定义的?

例如,像这样:

<form method="post" action="index.php" target="_parent">
...
</form>
<form method="post" action="index.php" name="login" target="_top" class="login">
// This is the form that I want to post data
...
<input value="Go" type="submit" id="input_go" />
</form>

修改

Anfurny提供的此解决方案不起作用:

post.PostItems.Add("login", "input_go");

我已经更新了代码,提交的按钮只定义了id。

EDIT2:

我找到了一个包含两种表单的页面的好例子。 phpMyAdmin demo,它有两种形式,一种是选择不同的语言,另一种是登录。这个演示有:

  • username = root
  • 密码为空

现在我尝试使用此代码以编程方式登录(它使用的是我在页面开头发布链接的类):

// First we create an instance of a class PostSubmitter.
PostSubmitter post = new PostSubmitter();

// We set the URL to which the POST should go.
post.Url = "http://demo.phpmyadmin.net/STABLE/index.php";

// We add items (password and username).
post.PostItems.Add("pma_username", "root");
post.PostItems.Add("pma_password", "");

// Also tried to add to which login form should post and the
// value set to id of the submit button, no luck.
//post.PostItems.Add("login_form", "input_go");

// Here we set the method.
post.Type = PostSubmitter.PostTypeEnum.Post;

// Getting back the result.
string result = post.Post();

// Writting it to the file.
TextWriter tw = new StreamWriter("C:/response.txt");
tw.WriteLine(result);
tw.Close();

response.txt文件与登录页面具有相同的内容,其中应该包含欢迎页面的代码。现在我该如何更改代码,成功登录?

2 个答案:

答案 0 :(得分:0)

通常当您将多个表单发布到同一个位置时,您可以使用单击的按钮区分它们。你可以不这样做,并在你的帖子值列表中添加一个虚拟按钮(动作)吗?

答案 1 :(得分:0)

你有没有试过这样的事情:

post.PostItems.Add("FORM_NAME","SUBMIT_BUTTON_NAME ");

当FORM_NAME是您要提交的表单的名称时,SUBMIT_BUTTON_NAME是您希望按下的提交按钮的名称(如果存在多个)。