用C# WebClient类 提交数据

用C# WebClient类 提交

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++) + "";

            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;
            }
          
        }

      
    }
}

原文地址:https://www.cnblogs.com/tiancai/p/2938687.html