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(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "远程主机IP:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "测试结果:";
//
// txt_IPAddress
//
this.txt_IPAddress.Location = new System.Drawing.Point(, );
this.txt_IPAddress.Name = "txt_IPAddress";
this.txt_IPAddress.Size = new System.Drawing.Size(, );
this.txt_IPAddress.TabIndex = ;
//
// lst_PingResult
//
this.lst_PingResult.FormattingEnabled = true;
this.lst_PingResult.ItemHeight = ;
this.lst_PingResult.Location = new System.Drawing.Point(, );
this.lst_PingResult.Name = "lst_PingResult";
this.lst_PingResult.Size = new System.Drawing.Size(, );
this.lst_PingResult.TabIndex = ;
//
// btn_StartPing
//
this.btn_StartPing.Location = new System.Drawing.Point(, );
this.btn_StartPing.Name = "btn_StartPing";
this.btn_StartPing.Size = new System.Drawing.Size(, );
this.btn_StartPing.TabIndex = ;
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(, );
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 = ;
//调用同步 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());
}
}
}