用C# WebClient类提交页面表单

发布时间:2010年07月01日      浏览次数:818 次
WebClient是一个强大的类。
提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。
这也就意味着可以像网站发送数据。
例如给网站留言,注册用户等等。。。。。。
下面是自动注册的一个功能
具体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace AutoAcquisitionF
{
public partial class Form1 : Form
{
int id = 20;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.timer1.Interval = int.Parse(this.intervalTime.Text);
this.timer1.Tick += new EventHandler(regusers);
this.id = int.Parse(this.idB.Text);
this.timer1.Start();
}
void regusers(object sender, EventArgs e)
{
// 要提交表单的URI字符串。
string uriString = "http://www.***.com/UserRegPost.asp";
Random r = new Random();
int rint = r.Next(1000);
string postString = "UserName=test" + (id++) + "&Password=111111&PwdConfirm=111111&Question=123456&Answer=2222&sex=1&Email=123@126.com&Add=shouasdasdfasdf&postcode=264000&Phone=0535-6719268";
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = Encoding.Default.GetBytes(postString);
byte[] responseData = webClient.UploadData(uriString, "POST", postData);
string srcString = Encoding.Default.GetString(responseData);
if (srcString.IndexOf("你注册的用户名") > 0)
{
this.html.Text = id+"注册成功" + DateTime.Now.ToString(); ;
}
if (id==int.Parse(this.idE.Text))
{
this.timer1.Stop();
this.timer1.Enabled = false;
}
}
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!