0

这段代码需要一段时间才能执行,尽管我需要做的只是从网站上获取一串文本。我现在有这个

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 秒,而且应该非常快。页面上只有文字。

4

1 回答 1

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();
于 2013-06-17T00:20:30.303 回答