这段代码需要30秒才能运行,很明显我做错了什么
本文关键字:运行 什么 错了 段代码 代码 30秒 | 更新日期: 2023-09-27 18:18:51
这段代码需要一段时间来执行,尽管我所需要做的就是从网站上获取一串文本。这里是
private void main_Load(object sender, EventArgs e)
{
string word = "1.5";
try
{
var url = "http://chipperyman573.com/rtf/textbot.html";
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string downloadedString;
while ((downloadedString = reader.ReadLine()) != null)
{
if (downloadedString == word)
{
//The stuff happens if there's no update
}
else
{
//The stuff that happens if there is an update
}
}
}
}
catch
{
//The stuff that happens if it can't connect to the webpage
}
}
这大约需要30秒的时间,应该是超级快的。页面上只有文字
您是否尝试过使用HttpWebRequest对象?WebClient是一个更高层次的包装器,它可能会带来一些你不需要的自由。只需要使用这段代码,它只需要一秒钟就可以运行…
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://chipperyman573.com/rtf/textbot.html");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String result = reader.ReadToEnd();