博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

C#编写网站测速 

WebClient wcl = new WebClient();
Stopwatch spwatch = new Stopwatch();
spwatch.Start();
byte[] resultBytes = wcl.DownloadData(new Uri(textBox1.Text));
spwatch.Stop();
MessageBox.Show(spwatch.Elapsed.ToString());

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace PingExample
{
    public partial class Form1 : Form
    {
        #region 设计
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txt_IPAddress = new System.Windows.Forms.TextBox();
            this.lst_PingResult = new System.Windows.Forms.ListBox();
            this.btn_StartPing = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(30, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "远程主机IP:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(30, 61);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "测试结果:";
            // 
            // txt_IPAddress
            // 
            this.txt_IPAddress.Location = new System.Drawing.Point(113, 21);
            this.txt_IPAddress.Name = "txt_IPAddress";
            this.txt_IPAddress.Size = new System.Drawing.Size(148, 21);
            this.txt_IPAddress.TabIndex = 2;
            // 
            // lst_PingResult
            // 
            this.lst_PingResult.FormattingEnabled = true;
            this.lst_PingResult.ItemHeight = 12;
            this.lst_PingResult.Location = new System.Drawing.Point(32, 87);
            this.lst_PingResult.Name = "lst_PingResult";
            this.lst_PingResult.Size = new System.Drawing.Size(229, 136);
            this.lst_PingResult.TabIndex = 3;
            // 
            // btn_StartPing
            // 
            this.btn_StartPing.Location = new System.Drawing.Point(105, 239);
            this.btn_StartPing.Name = "btn_StartPing";
            this.btn_StartPing.Size = new System.Drawing.Size(75, 23);
            this.btn_StartPing.TabIndex = 4;
            this.btn_StartPing.Text = "Ping";
            this.btn_StartPing.UseVisualStyleBackColor = true;
            this.btn_StartPing.Click += new System.EventHandler(this.btn_StartPing_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 284);
            this.Controls.Add(this.btn_StartPing);
            this.Controls.Add(this.lst_PingResult);
            this.Controls.Add(this.txt_IPAddress);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "ping 类的用法";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txt_IPAddress;
        private System.Windows.Forms.ListBox lst_PingResult;
        private System.Windows.Forms.Button btn_StartPing;
        #endregion


        public Form1()
        {
            InitializeComponent();
        }

        private void btn_StartPing_Click(object sender, EventArgs e)
        {
            this.lst_PingResult.Items.Clear();
            //远程服务器IP
            string ipStr = txt_IPAddress.Text.ToString().Trim();
            //构造Ping实例
            Ping pingSender = new Ping();
            //Ping 选项设置
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            //测试数据
            string data = "";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            //设置超时时间
            int timeout = 120;
            //调用同步 send 方法发送数据,将返回结果保存至PingReply实例
            PingReply reply = pingSender.Send(ipStr, timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                lst_PingResult.Items.Add("答复的主机地址:" + reply.Address.ToString());
                lst_PingResult.Items.Add("往返时间:" + reply.RoundtripTime);
                lst_PingResult.Items.Add("生存时间(TTL):" + reply.Options.Ttl);
                lst_PingResult.Items.Add("是否控制数据包的分段:" + reply.Options.DontFragment);
                lst_PingResult.Items.Add("缓冲区大小:" + reply.Buffer.Length);
            }
            else
                lst_PingResult.Items.Add(reply.Status.ToString());
        }
    }
}