C# 自动填写 ASPX 页面文本框并提交 - 重复操作
下面是使用 C# 自动填写和提交一个 ASPX 页面文本框的代码示例,可以重复指定次数进行填写和提交操作:
using System;
using System.Net;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
// 设置重复填写和提交的次数
int repeatCount = 10;
// 循环执行填写和提交操作
for (int i = 0; i < repeatCount; i++)
{
// 创建 HTTP 请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create('http://example.com/your-page.aspx');
request.Method = "POST";
// 设置 POST 数据(假设有一个名为 'textbox1' 的文本框)
string postData = "textbox1=your-value";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
// 发送 POST 数据
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
// 获取服务器响应
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// 读取响应内容
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseText = reader.ReadToEnd();
Console.WriteLine(responseText);
}
}
}
}
Console.ReadLine();
}
}
在上面的代码中,你需要将 'http://example.com/your-page.aspx' 替换为你要填写和提交的 ASPX 页面的 URL。并且根据你的实际情况,更改 POST 数据中的文本框名称和值。
在代码中,我们使用 HttpWebRequest 类来发送带有 POST 数据的 HTTP 请求,并使用 HttpWebResponse 类来获取服务器的响应。我们可以将这段代码放在一个循环中,根据需要重复执行填写和提交操作的次数。
请注意,这段代码仅适用于简单的表单提交,如果有复杂的页面逻辑(例如需要处理视图状态或身份验证等),还需要根据实际情况进行修改。
原文地址: https://www.cveoy.top/t/topic/qqR8 著作权归作者所有。请勿转载和采集!