首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HttpClient下载CSV文件并使用CsvHelper即时处理

使用HttpClient下载CSV文件并使用CsvHelper即时处理
EN

Stack Overflow用户
提问于 2020-08-05 18:45:17
回答 1查看 1.1K关注 0票数 0

我正在尝试使用HttpClient下载csv文件,并使用CsvHelper库进行处理

代码语言:javascript
复制
string url = "https://www.nseindia.com/api/allIndices?csv=true";
string useragent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36";

var httpclient = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Get, url);
var sendtask = httpclient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var response = sendtask.Result.EnsureSuccessStatusCode();
var httpStream = await response.Content.ReadAsStreamAsync();
StreamReader streamreader = new StreamReader(httpStream);

CsvReader futureoptionsreader = new CsvReader(streamreader, CultureInfo.CreateSpecificCulture("en-US"));
futureoptionsreader.Configuration.RegisterClassMap<MappingNSEIndexes>();
var list = futureoptionsreader.GetRecords<RawNSEIndexes>();
var number = list.Count();

以错误数据结束

代码语言:javascript
复制
CsvHelper.BadDataException: You can ignore bad data by setting BadDataFound to null.
   at CsvHelper.CsvParser.Read()
   at CsvHelper.CsvReader.Read()
   at CsvHelper.CsvReader.GetRecords[T]()+MoveNext()
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at MIS.Equity.API.Controllers.BooksController.GetCompaniesAsync() in C:\Users\Ramesh\source\repos\MIS_Main\MarkertInfoSystem\MIS.Equity.API\Controllers\BooksController.cs:line 66
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)

这种方法正确吗?提前感谢

EN

回答 1

Stack Overflow用户

发布于 2020-08-08 22:52:00

所以,你发布的代码有点...我无法编译它,所以我重写了它,使其在使用HttpClient时遵循一个通用的模式

代码语言:javascript
复制
var url = "https://www.nseindia.com/api/allIndices?csv=true";

using (var msg = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
{
    msg.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/csv"));
    using (var resp = await _client.SendAsync(msg))
    {
        resp.EnsureSuccessStatusCode();

        using (var s = await resp.Content.ReadAsStreamAsync())
        using (var sr = new StreamReader(s))
        using (var futureoptionsreader = new CsvReader(sr, CultureInfo.CurrentCulture))
        {
            futureoptionsreader.Configuration.RegisterClassMap<MappingNSEIndexes>();
            var list = futureoptionsreader.GetRecords<RawNSEIndexes>();
            var number = list.Count();
        }
    }
}

请记住,除非您添加Headers.Accept行,否则服务器不会返回任何数据。

其中一个主要问题是这一行:

代码语言:javascript
复制
var response = sendtask.Result.EnsureSuccessStatusCode();
var httpStream = await response.Content.ReadAsStreamAsync();

这甚至不能编译-- EnsureSuccessStatusCode返回void。因此,我假设您在尝试进行故障排除时修改了代码,然后复制并粘贴了代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63263487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档