我发现this great class发送POST请求到一个网页。现在我在单个表单的测试页上尝试了它,它可以工作。但现在我想对页面执行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,它有两种形式,一种是用于选择不同的语言,另一种是用于登录。该演示有:
- 用户名=根
- 密码为空
现在我试图用这个代码编程方式登录(它使用了我已张贴在页面开始链接类):
// 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文件具有与登录页面相同的内容,它应具有欢迎页面的代码。现在我该如何更改代码,才能成功登录?
没有,不起作用,因为我已经在上面的评论中解释过了。 –
奇怪。您已将所有Request.Form值记录到记录器或文件以查看正在经历的内容?抱歉,我对你的按钮问题有点困惑。我不认为你的问题中的实际表单与C#表单发布类的内容有什么关系。如果出于某种奇怪的原因,它会。为该按钮添加一个NAME属性。 Id不会发布。 –
无法更改页面的内容,因此无法向该按钮添加名称属性。我已经将返回的字符串打印到txt文件中,它实际上返回页面的源代码,但它应该返回带有错误附带的源代码(插入的数据不正确)。所以看起来请求没有被正确发送。 –