C# 您如何以编程方式填写表单并“发布”网页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:16:24  来源:igfitidea点击:

How do you programmatically fill in a form and 'POST' a web page?

提问by Guy

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?

使用 C# 和 ASP.NET 我想以编程方式填充网页(表单)上的一些值(4 个文本框),然后“发布”这些值。我该怎么做呢?

Edit: 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.

编辑:澄清:有一项服务 (www.stopforumspam.com),您可以在其“添加”页面上提交 IP、用户名和电子邮件地址。我希望能够在我的网站页面上创建一个链接/按钮来填充这些值并提交信息,而无需复制/粘贴它们并单击提交按钮。

Further clarification: How do automated spam bots fill out forms and click the submit button if they were written in C#?

进一步说明:如果自动垃圾邮件机器人是用 C# 编写的,它们如何填写表单并单击提交按钮?

采纳答案by Ryan Farley

The code will look something like this:

代码将如下所示:

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();

回答by Ryan Rinaldi

View the source of the page and use the WebRequest class to do the posting. No need to drive IE. Just figure out what IE is sending to the server and replicate that. Using a tool like Fiddler will make it even easier.

查看页面源码,使用WebRequest类进行发帖。无需驱动IE。只需弄清楚 IE 发送到服务器的内容并复制它。使用像 Fiddler 这样的工具会更容易。

回答by John Kelvie

You can use the UploadValues method on WebClient - all it requires is passing a URL and a NameValueCollection. It is the easiest approach that I have found, and the MS documentation has a nice example:
http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx

您可以在 WebClient 上使用 UploadValues 方法 - 它所需要的只是传递一个 URL 和一个 NameValueCollection。这是我发现的最简单的方法,MS 文档有一个很好的例子:http:
//msdn.microsoft.com/en-us/library/9w7b4fz7.aspx

Here is a simple version with some error handling:

这是一个带有一些错误处理的简单版本:

var webClient = new WebClient();
Debug.Info("PostingForm: " + url);
try
{
     byte [] responseArray = webClient.UploadValues(url, nameValueCollection);
     return new Response(responseArray, (int) HttpStatusCode.OK);
}
catch (WebException e)
{
     var response = (HttpWebResponse)e.Response;
     byte[] responseBytes = IOUtil.StreamToBytes(response.GetResponseStream());
     return new Response(responseBytes, (int) response.StatusCode);
}  

The Response class is a simple wrapper for the response body and status code.

Response 类是响应正文和状态代码的简单包装器。

回答by John

I had a situation where I needed to post free text from a html textarea programmatically and I had issues where I was getting <br />in my param list i was building.

我遇到了一种情况,我需要以编程方式从 html textarea 发布自由文本,并且<br />在我正在构建的参数列表中遇到问题。

My solution was a replace of the br tags with linebreak characters and htmlencoding just to be safe.

为了安全起见,我的解决方案是用换行符和 htmlencoding 替换 br 标签。

Regex.Replace( HttpUtility.HtmlDecode( test ), "(<br.*?>)", "\r\n" ,RegexOptions.IgnoreCase);

回答by user3887407

Where you encode the string:

对字符串进行编码的位置:

Encoding.Default.GetBytes(postData);

Encoding.Default.GetBytes(postData);

Use Ascii instead for the google apis:

使用 Ascii 代替 google api:

Encoding.ASCII.GetBytes(postData);

Encoding.ASCII.GetBytes(postData);

this makes your request the same as and equivalent "curl --data "..." [url]" request

这使您的请求与等效的“curl --data”...“[url]”请求相同

回答by Sadid Khan

you can send a post/get request with many ways. Different types of library is there to help. I found it is confusingto choose which one I should use and what are the differencesamong them.

您可以通过多种方式发送 post/get 请求。不同类型的图书馆可以提供帮助。我发现选择我应该使用哪个以及它们之间有什么区别令人困惑

After surfing stack overflow this is the best answer I found. this thread explains all

在浏览堆栈溢出后,这是我找到最佳答案。这个线程解释了所有

https://stackoverflow.com/a/4015346/1999720

https://stackoverflow.com/a/4015346/1999720