您如何以编程方式填写表单并“发布"网页? [英] How do you programmatically fill in a form and 'POST' a web page?
问题描述
使用 C# 和 ASP.NET,我想以编程方式填充网页(表单)上的一些值(4 个文本框),然后发布"这些值.我该怎么做?
Using C# and ASP.NET I want to programmatically fill in some values (4 text boxes) on a web page (form) and then 'POST' those values. How do I do this?
澄清:有一项服务 (www.stopforumspam.com),您可以在其添加"页面上提交 IP、用户名和电子邮件地址.我希望能够在我的网站页面上创建一个链接/按钮来填充这些值并提交信息,而无需复制/粘贴它们并单击提交按钮.
Clarification: There is a service (www.stopforumspam.com) where you can submit ip, username and email address on their 'add' page. I want to be able to create a link/button on my site's page that will fill in those values and submit the info without having to copy/paste them across and click the submit button.
进一步说明:如果表单是用 C# 编写的,自动垃圾邮件机器人如何填写表单并单击提交按钮?
Further clarification: How do automated spam bots fill out forms and click the submit button if they were written in C#?
推荐答案
代码看起来像这样:
WebRequest req = WebRequest.Create("http://mysite/myform.aspx");
string postData = "item1=11111&item2=22222&Item3=33333";
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
这篇关于您如何以编程方式填写表单并“发布"网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!