此代码需要一段时间才能执行,即使我需要它只是从网站上获取一串文本。我现在有这个
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秒才能发生,它应该超级快。该页面上只有文字。
答案 0 :(得分:6)
您是否尝试过使用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();