我有这个:
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/?qt=edismax_nosyn_a2aperson&q=(huibert faro)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1 OR fk_account:(10847871))&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1 OR fk_account:(10847871))&spellcheck=true&spellcheck.count=-3&start=10&sort=name_last asc, score desc&omitHeader=true");
System.Diagnostics.Stopwatch timer = new Stopwatch();
timer.Start();
TimeSpan timeTaken = timer.Elapsed;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
timer.Stop();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
try
{
using (StreamReader sr = new StreamReader("testSolar.txt"))
{
String line = sr.ReadToEnd();
txtReadFile.Text = timeTaken.ToString();
}
}
catch (Exception exception)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(exception.Message);
}
}
但是:HttpWebRequest request = (HttpWebRequest)WebRequest.Create
现在是硬编码的。但我有一个包含许多请求的文本文件,如下所示:
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=standard_a2aperson&q=*:*&fq=(nosyn_name_last_exact:(qxqroelofqxq))&fq=(nosyn_name_patronym_exact:(qxqharmsqxq))&spellcheck.q=(qxqroelofqxq qxqharmsqxq)&fq={!tag%3Dalldoctypes}doc_type:1&fq=date_main:[0 TO 18133112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=edismax_nosyn_a2aperson&q=(arie van Hattum)&fq={!tag%3Dalldoctypes}doc_type:1&fq=(fk_collectiontype:6)&fq=(fk_eventrole:63)&fq=date_main:[18430000 TO 19513112]&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=edismax_nosyn_a2aperson&q=(huibert faro)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1 OR fk_account:(10847871))&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1 OR fk_account:(10847871))&spellcheck=true&spellcheck.count=-3&start=10&sort=name_last asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=edismax_nosyn_a2aperson&q=(huibert faro)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1 OR fk_account:(10847871))&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1 OR fk_account:(10847871))&spellcheck=true&spellcheck.count=-3&start=10&sort=name_last asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=edismax_nosyn_a2aperson&q=(ickson)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=false&echoParams=EXPLICIT
http://10.123.9.66:80/solr_3.6/wiewaswie_live/select/ qt=edismax_nosyn_a2aperson&q=(ickson)&fq={!tag%3Dalldoctypes}doc_type:1&facet.query={!ex%3Dalldoctypes}doc_type:3 AND (b_public:1)&facet.query={!ex%3Dalldoctypes}doc_type:2 AND (b_public:1)&spellcheck=true&spellcheck.count=-3&start=0&sort=name_last asc, score desc&omitHeader=false&echoParams=EXPLICIT
http://10.123.9.66:80/solr_3.6/combi_live/select/ qt=standard_a2aperson&q=(((((nosyn_name_last_b_exact:(qxqkuilenburgqxq))))))&fq=(nosyn_name_last_exact:(qxqbroekqxq))&spellcheck.q=(qxqbroekqxq kuilenburg)&fq=(fk_collectiontype:6)&spellcheck=true&spellcheck.count=-3&start=10&sort=date_main asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/combi_live/select/ qt=standard_a2aperson&q=(((((nosyn_name_last_b_exact:(qxqkuilenburgqxq))))))&fq=(nosyn_name_last_exact:(qxqbroekqxq))&spellcheck.q=(qxqbroekqxq kuilenburg)&fq=(fk_collectiontype:6)&spellcheck=true&spellcheck.count=-3&start=10&sort=date_main asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/combi_live/select/ qt=standard_a2aperson&q=(((((nosyn_name_last_b_exact:(qxqkruijsqxq))))))&fq=(nosyn_name_first_exact:(qxqw*qxq))&fq=(nosyn_name_last_exact:(qxqtreurenqxq))&spellcheck.q=(qxqw*qxq qxqtreurenqxq kruijs)&spellcheck=true&spellcheck.count=-3&start=0&sort=date_main asc, score desc&omitHeader=true
http://10.123.9.66:80/solr_3.6/combi_live/select/ qt=standard_a2aperson&q=(((((nosyn_name_last_b_exact:(qxqkruijsqxq))))))&fq=(nosyn_name_first_exact:(qxqw*qxq))&fq=(nosyn_name_last_exact:(qxqtreurenqxq))&spellcheck.q=(qxqw*qxq qxqtreurenqxq kruijs)&spellcheck=true&spellcheck.count=-3&start=0&sort=date_main asc, score desc&omitHeader=true
所以从每个http我想看到文本框中的响应时间():txtReadFile.Text = timeTaken.ToString();
谢谢。
答案 0 :(得分:0)
首先,您使用StopWatch
类错了。你应该启动它,停止它然后获得经过的时间。
接下来,如果您尝试读取该文件并使用该文件中的网址,则需要先读取文件,然后尝试连接到网址。
以下是我认为你想要的,作为一个控制台应用程序。如果您想在WinForm应用程序中使用它,您可以轻松使用Dictionary<string, TimeSpan>
填充数据网格或列表视图,以获得更友好的查看界面。
//Consider making this configurable
const string sourceFile = @"testSolar.txt";
var requester = new WebClient();
var times = new Dictionary<string, TimeSpan>();
var stopwatch = new System.Diagnostics.Stopwatch();
//Add header so if headers are tracked, it will show it is your application rather than something ambiguous
requester.Headers.Add(HttpRequestHeader.UserAgent, "Response-Tester-Client");
var urlList = new List<string>();
//Loop through the lines in the file to get the urls to hit against
try
{
stopwatch.Start();
using (var reader = new StreamReader(sourceFile))
{
while (!reader.EndOfStream)
{
urlList.Add(reader.ReadLine());
}
}
}
catch (Exception e)
{
Console.WriteLine("An error occured while attempting to access the source file at {0}", sourceFile);
}
finally
{
//Stop, record and reset the stopwatch
stopwatch.Stop();
times.Add("FileReadTime", stopwatch.Elapsed);
stopwatch.Reset();
}
//Try to connect to each url
var counter = 1;
foreach (var url in urlList)
{
try
{
stopwatch.Start();
requester.DownloadString(url);
}
catch (Exception e)
{
Console.WriteLine("An error occured while attempting to connect to {0}", url);
}
finally
{
stopwatch.Stop();
//We use the counter for a friendlier url as the current ones are unwieldly
times.Add("Url " + counter, stopwatch.Elapsed);
counter++;
stopwatch.Reset();
}
}
//Release the resources for the WebClient
requester.Dispose();
//Write the response times
foreach (var key in times.Keys)
{
Console.WriteLine("{0}: {1}", key, times[key].TotalSeconds);
}