简易实现网络Post压力测试器:
一般get测试比较多,post测试工具比较少,有也不符合要求,自己弄一个测试。
界面如下:
源码如下:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace postHttp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string url = "";
string filePath = "";
int tryCount = 0;
int spac = 0;
int SuccessCount = 0;
int FailCount = 0;
string result = "";
//打开
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtFilePath.Text = openFileDialog1.FileName;
}
//发送
private void button2_Click(object sender, EventArgs e)
{
SuccessCount = 0;
FailCount = 0;
result = "";
url = txtUrl.Text;
filePath = txtFilePath.Text;
int tn = (int)numThreads.Value;
tryCount = (int)numTrys.Value;
spac = int.Parse(txtSpac.Text);
//使用cp提供的下行包发送测试
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader reader1 = new StreamReader(fs, Encoding.Default);
result = "[" + DateTime.Now.ToString() + "]\r\n发送数据包:\r\n" + reader1.ReadToEnd() +
"\r\n------------------------------------------------\r\n\r\n";
reader1.Close();
for (int i = 0; i < tn; i++)
{
Thread td = new Thread(new ThreadStart(ThreadFun));
td.Start();
}
while ((SuccessCount + FailCount) < tn * tryCount)
{
Thread.Sleep(10);
msg.Text = result;
}
msg.Text += "\r\n-------------------------------------------------------------";
msg.Text += "\r\n成功:"+SuccessCount;
msg.Text += "\r\n失败:"+FailCount;
msg.Text += "\r\n成功率:" + string.Format("{0:p}", ((double)SuccessCount /(SuccessCount + FailCount)));
}
public void ThreadFun()
{
for (int j = 0; j < tryCount; j++)
{
PostOneData();
Thread.Sleep(spac);
}
}
public void PostOneData()
{
bool success = false;
try
{
string sendData = "";
//使用cp提供的下行包发送测试
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader reader1 = new StreamReader(fs, Encoding.Default);
byte[] postData = System.Text.Encoding.UTF8.GetBytes(reader1.ReadToEnd());
reader1.Close();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "text/xml;charset=\"utf-8\""; //
myRequest.ContentLength = postData.Length;
Stream newStream = myRequest.GetRequestStream();
//发送数据
newStream.Write(postData, 0, postData.Length);
newStream.Close();
//获取返回的结果
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retStr = reader.ReadToEnd();
result += "["+DateTime.Now.ToString()+"]\r\n接收到数据包:\r\n"+retStr+"\r\n\r\n";
//if (retStr.Length>0)
if(response.StatusCode==HttpStatusCode.OK)
success = true;
reader.Close();
response.Close();
}
catch
{
success = false;
}
if (success)
SuccessCount++;
else
FailCount++;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace postHttp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string url = "";
string filePath = "";
int tryCount = 0;
int spac = 0;
int SuccessCount = 0;
int FailCount = 0;
string result = "";
//打开
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtFilePath.Text = openFileDialog1.FileName;
}
//发送
private void button2_Click(object sender, EventArgs e)
{
SuccessCount = 0;
FailCount = 0;
result = "";
url = txtUrl.Text;
filePath = txtFilePath.Text;
int tn = (int)numThreads.Value;
tryCount = (int)numTrys.Value;
spac = int.Parse(txtSpac.Text);
//使用cp提供的下行包发送测试
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader reader1 = new StreamReader(fs, Encoding.Default);
result = "[" + DateTime.Now.ToString() + "]\r\n发送数据包:\r\n" + reader1.ReadToEnd() +
"\r\n------------------------------------------------\r\n\r\n";
reader1.Close();
for (int i = 0; i < tn; i++)
{
Thread td = new Thread(new ThreadStart(ThreadFun));
td.Start();
}
while ((SuccessCount + FailCount) < tn * tryCount)
{
Thread.Sleep(10);
msg.Text = result;
}
msg.Text += "\r\n-------------------------------------------------------------";
msg.Text += "\r\n成功:"+SuccessCount;
msg.Text += "\r\n失败:"+FailCount;
msg.Text += "\r\n成功率:" + string.Format("{0:p}", ((double)SuccessCount /(SuccessCount + FailCount)));
}
public void ThreadFun()
{
for (int j = 0; j < tryCount; j++)
{
PostOneData();
Thread.Sleep(spac);
}
}
public void PostOneData()
{
bool success = false;
try
{
string sendData = "";
//使用cp提供的下行包发送测试
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader reader1 = new StreamReader(fs, Encoding.Default);
byte[] postData = System.Text.Encoding.UTF8.GetBytes(reader1.ReadToEnd());
reader1.Close();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "text/xml;charset=\"utf-8\""; //
myRequest.ContentLength = postData.Length;
Stream newStream = myRequest.GetRequestStream();
//发送数据
newStream.Write(postData, 0, postData.Length);
newStream.Close();
//获取返回的结果
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retStr = reader.ReadToEnd();
result += "["+DateTime.Now.ToString()+"]\r\n接收到数据包:\r\n"+retStr+"\r\n\r\n";
//if (retStr.Length>0)
if(response.StatusCode==HttpStatusCode.OK)
success = true;
reader.Close();
response.Close();
}
catch
{
success = false;
}
if (success)
SuccessCount++;
else
FailCount++;
}
}
}