减少HttpWebRequest时间或逐行读取网站

时间:2016-06-21 21:27:26

标签: c# httpwebrequest

原始问题

我正在为游戏服务器编写远程连接。它需要读取日志文件以确定服务器中发生的情况。我写了一个方法来读取日志文件。目前需要7720毫秒才能完成。

    private string getlog()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(logFileLocation);
        webRequest.UserAgent = ".NET Framework Test Client";
        webRequest.Accept = "text/html";
        HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
        string responseData;
        using (StreamReader responseReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            responseData = responseReader.ReadToEnd();
        }
        return responseData;
    }

我正在尝试减少此方法运行的时间。从最旧的信息开始,它所获得的文件大约是7000行。我根本不需要旧的信息,所以我无缘无故地阅读它。有没有办法逐行读取文件。或者我有办法加速这种方法吗?

修改

代码现在

    private string getlog()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(logFileLocation);
        webRequest.UserAgent = ".NET Framework Test Client";
        webRequest.Accept = "text/html";
        webRequest.Proxy = null; //Thanks to tsandy
        HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
        string responseData;
        using (StreamReader responseReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            responseData = responseReader.ReadToEnd();
        }
        return responseData;
    }

1 个答案:

答案 0 :(得分:2)

代理自动检测很慢webRequest.Proxy = null;