aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebTest.aspx.cs" Inherits="WebTest" %><%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试工具BS.beta1.0版</title>
<style type="text/css">
.myTable, .myTable td
{
border: 1px solid #cccccc;
border-collapse: collapse;
width: 740px;
margin: 0 auto;
}
.myTable1
{
width: 800px;
margin: 0 auto;
}
</style>
<script type="text/javascript" src="JS/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript" src="JS/JScript.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="font-size: 12px; text-align: center;">
<div id="DIVTOP">
<table class="myTable1">
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" Width="220px" />
</td>
<td>
<asp:Button ID="btnStartTest" runat="server" Text="开始测试" OnClick="btnStartTest_Click" />
</td>
<td style="width: 418px;">
<div id="DivFile">
</div>
</td>
</tr>
</table>
</div>
<div id="DIVCENTER" style="text-align: center; margin-top: 20px; margin-bottom: 20px;">
<asp:Label ID="lbltime" runat="server" Font-Size="Small" ForeColor="#FF66FF"></asp:Label>
<table class="myTable">
<tr>
<td>
选择
</td>
<td>
URL
</td>
<td>
返回结果
</td>
<td>
发送时间
</td>
<td>
返回时间
</td>
</tr>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Text='<%# Eval("IDurl")%>' />
</td>
<td style="font-size: 13px; text-align: left;">
<%# Eval("testurl")%>
</td>
<td>
<%# Eval("RepeatText") %>
</td>
<td>
<%# Eval("StartTime") %>
</td>
<td>
<%# Eval("ENDTime") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblEmpty" Text="列表暂无数据!" runat="server" Visible='<%#bool.Parse((Repeater1.Items.Count==0).ToString())%>'>
</asp:Label>
</FooterTemplate>
</asp:Repeater>
</table>
</div>
<div id="DIVADD" style="">
<table class="myTable1">
<tr>
<td>
<asp:TextBox ID="txtURLText" runat="server" Width="220px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnAddURL" runat="server" Text="添加地址" OnClick="btnAddURL_Click" />
</td>
<td style="width: 418px;">
<div id="DIVBTNADDURL">
</div>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Net;
using System.Data;
using System.Text;
using Newding.GeneralClass;
using System.Collections;public partial class WebTest : System.Web.UI.Page
{
protected string RepeaterURL;
protected string RepeatText;//得到返回的XML内容
protected string StartTime;//开始测试的时间
protected string ENDTime;//结束测试的时间
protected DataSet ds;
protected string CheckUrlId;//被选中的URL的ID
protected string[] xmlUrlList = null;
protected void Page_Load(object sender, EventArgs e)
{
try
{
#region XML转换成Datatable
//XmlDocument doc = new XmlDocument();
//doc.Load(Server.MapPath("URL.xml"));
//XmlNodeList xml = doc.GetElementsByTagName("URL");
//for (int i = 0; i < xml.Count; i++)
//{
// dt = new DataTable();
// string testurl = xml.Item(i).Attributes["testurl"].Value;
// string IDurl = xml.Item(i).Attributes["IDurl"].Value;
// DataColumn colu = new DataColumn();
// colu.DataType = System.Type.GetType("System.Decimal");
// colu.AllowDBNull = false;
// dt.Columns.Add(new DataColumn(testurl, Type.GetType("System.Decimal")));
// dt.Columns.Add(new DataColumn(IDurl, Type.GetType("System.Decimal")));
// Repeater1.DataSource = dt.Columns[0];
// Repeater1.DataBind();
//}
#endregion
ds = new DataSet();
ds.ReadXml(Server.MapPath("URL.xml"));
//给内存中的表添加3列
ds.Tables[0].Columns.Add("RepeatText", System.Type.GetType("System.String"));
ds.Tables[0].Columns.Add("StartTime", System.Type.GetType("System.String"));
ds.Tables[0].Columns.Add("ENDTime", System.Type.GetType("System.String"));
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
catch (Exception)
{
throw;
}
} /// <summary>
/// 开始测试的时候读取FileUpload选择的文件的内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnStartTest_Click(object sender, EventArgs e)
{
try
{
string AllPath = FileUpload1.FileName.ToString();//选择文件的路径
string PathText = null;//选定文件的内容
FormatConversion fcsion = new FormatConversion();//实例化加解密工具
if (AllPath.ToString() != "")
{
if (AllPath.Substring(AllPath.LastIndexOf("."), 4).ToUpper() == ".TXT")
{
//定义一个线程,并且启动
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
System.Threading.Thread.Sleep(1000);
PathText = GetUrl(AllPath, PathText, fcsion);
if (xmlUrlList.Length > 0)
{
GetRequest(PathText, fcsion);
watch.Stop();//结束线程
lbltime.Text = "整个过程共耗时:" + watch.ElapsedMilliseconds.ToString() + "毫秒";
Repeater1.DataBind();
}
}
else
{
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请选择.TXT文件!')</script>");
}
}
}
catch (Exception)
{
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('发生未知错误!')</script>");
} } private string GetUrl(string AllPath, string PathText, FormatConversion fcsion)
{
FileUpload1.SaveAs(Server.MapPath("TXT/" + AllPath));//先保存到服务器
StreamReader sr = new StreamReader(Server.MapPath("TXT/" + AllPath));//读取,貌似这里不稳定???????
PathText = sr.ReadToEnd();
PathText = fcsion.EncoderBase64(PathText);//加密?
string[] xmlUrlIDList = Session["checkUrlId"].ToString().Split(new Char[] { ',' });//定义UrlId的集合
string xmlUrl = null;
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)//通过UrlId循环读取URL
{
if (xmlUrlIDList.Contains(row["IDurl"].ToString()))
{
xmlUrl += row["testurl"].ToString() + "-";
}
}
string xmlUrl1 = xmlUrl.Substring(0, xmlUrl.Length - 1);//去掉最后一个 -
xmlUrlList = xmlUrl1.ToString().Split(new Char[] { '-' });
return PathText;
} private void GetRequest(string PathText, FormatConversion fcsion)
{
string url = null;//定义要测试的URL
for (int i = 0; i < xmlUrlList.Length; i++)
{
url = xmlUrlList[i] + "?request=" + PathText;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);//创建一个HttpWebRequest对象
HttpWebResponse response = (HttpWebResponse)req.GetResponse();//根据创建的HttpWebRequest来创建HttpWebResponse对象
Stream mystream = response.GetResponseStream();//取得返回后的stream
StreamReader mystreamReader = new StreamReader(mystream);//把Stream转换为StreamReader
string strtxt = mystreamReader.ReadToEnd();//读取出返回的内容
AddData(fcsion, i, response, strtxt);
}
}
//把接受到的数据保存到内存
private void AddData(FormatConversion fcsion, int i, HttpWebResponse response, string strtxt)
{
RepeatText = fcsion.DecoderBase64(strtxt); //解密返回的内容
StartTime = DateTime.Now.ToString() + DateTime.Now.Millisecond;
ENDTime = response.LastModified.ToString();
ds.Tables[0].Rows[i].SetField("RepeatText", RepeatText);
ds.Tables[0].Rows[i].SetField("StartTime", StartTime);
ds.Tables[0].Rows[i].SetField("ENDTime", ENDTime);
}
/// <summary>
/// 给XML中添加URL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAddURL_Click(object sender, EventArgs e)
{
try
{
XmlDocument xmldoc = new XmlDocument();
string mapPath = Server.MapPath("URL.xml");
xmldoc.Load(mapPath);
XmlNode node = xmldoc.SelectSingleNode("URLWrite");//读取URLWrite节点
XmlElement element = xmldoc.CreateElement("URL");
if (txtURLText.Text == "")
{
return;
}
else
{
int InsertUrlId = 0;
//循环遍历,读取最大值
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (Convert.ToInt32(dr["IDurl"]) >= InsertUrlId)
{
InsertUrlId = Convert.ToInt32(dr["IDurl"]);
}
}
element.SetAttribute("testurl", txtURLText.Text.Trim().ToString());
element.SetAttribute("IDurl", (InsertUrlId + 1).ToString());
node.AppendChild(element);
xmldoc.Save(mapPath);
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('添加成功!'),window.location.href='WebTest.aspx'</script>");
}
}
catch
{
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('发生未知错误!')</script>");
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
CheckBox cb = (CheckBox)e.Item.FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked == true)
{
CheckUrlId += cb.Text + ",";
}
}
if (CheckUrlId != null)
{
Session["CheckUrlId"] = CheckUrlId.Substring(0, CheckUrlId.Length - 1);
}
}
}
请大家发表评论!