简体   繁体   English

从C#中的多个Web文件读取第一行的有效方法

[英]Efficient way to read first lines from several web files in C#

There is a web server that contains several long files (>500Kb) with different categories of news. 有一个Web服务器,其中包含带有不同新闻类别的多个长文件(> 500Kb)。 I only need to get the latest news in each category, first lines of the file (usually < 1Kb but can be more) and to make the download quick and because the connection is slow, my idea is to read line by line so downloading the minimum amount of data. 我只需要获取每个类别的最新新闻,即文件的第一行(通常<1Kb,但可以更多),并使下载速度更快,并且由于连接速度较慢,我的想法是逐行阅读,因此下载最小数据量。 At the moment I executing the below code but for the time it takes does not seem much improvement from downloading the full file. 目前,我执行以下代码,但是从下载完整文件来看,这似乎并没有太大的改善。

foreach(var newsType in newsTypes)
{
    var request = WebRequest.Create("http://www.xxxx.com/" + newsType) as HttpWebRequest;
    request.Timeout = 5000;
    using (var response = request.GetResponse() as HttpWebResponse)
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            while (!reader.EndOfStream)
            {
                var fileRowCSV = reader.ReadLine();
                ...
                if (old-news) break;
                ...
        }
    }
}

Is there and setting I need to modify so not all the file gets transferred? 是否存在需要修改的设置,因此并非所有文件都可以传输? Can I reuse one connection in some way that can read the other files? 我可以某种方式重用一个连接以读取其他文件吗? Any ideas on how to improve the process? 关于如何改进流程的任何想法?

Thanks 谢谢

I don't think there's a built-in way to do this in the .NET Framework. 我认为.NET Framework中没有内置的方法可以做到这一点。 However, if you're looking to simply speed up the entire process, you can (relatively easily, I think) parallelize the outer foreach loop. 但是,如果您只是想加快整个过程的速度,则可以(相对容易地,我认为)并行化外部foreach循环。

Alternatively, here is an MSDN example that displays the progress of downloading a file from the web. 另外, 是一个MSDN示例,显示了从Web下载文件的进度。 You should be able to use the code in that example to accomplish what you want: after every X bytes, check if you've reached old-news and, if so, cancel the download. 您应该能够使用该示例中的代码来完成所需的工作:每X个字节之后,检查是否已收到old-news ,如果是,则取消下载。 Fair warning, it's not just a few lines of code. 合理的警告,不仅是几行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#使用StreamReader读几行? - C# Read several lines with StreamReader? 从COM流读取到c#byte []的内存有效方法 - memory efficient way to read from COM stream to c# byte[] 在C#中分割CSV文件的有效方法 - Efficient way to split CSV files in c# 如何有效地从儿童Pocress的标准品中读取长行? - How to read long lines from child pocress' stdout in an efficient way? 从C#编写Excel的有效方法 - Efficient way to write excel from C# C# Azure 存储队列 - 从队列中读取消息的更有效方式 - C# Azure Storage Queue - More efficient way to read messages from queue 从 C# 中的字节数组中读取字节的最有效方法是什么 - what is the most efficient way to read bytes from a byte array in C# 在C#中从文本文件中读取大量整数(双精度)的最有效方法是什么? - What is the most efficient way to read a lot of integers (doubles) from text file in C#? 使用SqlDataReader(C#)从SQL Server读取许多字节的最有效方法是什么 - What is the most efficient way to read many bytes from SQL Server using SqlDataReader (C#) 有没有一种有效的方法可以将C#类增量从Web api 2.2应用传播到测试器应用? - is there an efficient way to propogate c# class deltas from a web api 2.2 app to a tester app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM