zoukankan      html  css  js  c++  java
  • 用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;
                }
              
            }

          
        }
    }

  • 相关阅读:
    c中的数组与字符串
    c中的函数
    C中的流程控制
    c中的基本运算
    scanf函数
    c中的数据类型、常量、变量
    c中的关键字、标识符、注释
    ios必须知道的事情
    安卓开发之获取SD卡空间数据
    安卓日志猫的使用
  • 原文地址:https://www.cnblogs.com/tiancai/p/2938687.html
Copyright © 2011-2022 走看看