c# HttpClient 解析来自 webservice 的响应

发布于 2025-01-09 06:04:26 字数 1425 浏览 0 评论 0原文

我正在调用一个超级旧的外部网络服务,它在响应中包含 CSV 值:

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <[email protected]>
Content-Disposition: form-data;filename="MainDocument.bin";name="MainDocument"
Content-Type: application/json
Content-Description: MainDocument

{"mt_csvfile":"csvfile.csv"}
--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <csvfile.csv>
Content-Disposition: form-data;filename="csvfile.csv";name="csvfile.csv"
Content-Type: mimeType text/comma-separated-values;charset="uft-8"
Content-Description: csvfile.csv

"NUMBER","DESCRIPTION",
"00000000001","DESC 1",
"00000000002","DESC 2",
"00000000003","DESC 3",

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END--

我关心的是 CSV 有效负载。

如何解析响应? 我不是问如何解析 CSV 文件。

我正在使用httpClient:

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
    // need to get CSV values only
}

I am calling a super old external webservice which has CSV values inside the response:

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <[email protected]>
Content-Disposition: form-data;filename="MainDocument.bin";name="MainDocument"
Content-Type: application/json
Content-Description: MainDocument

{"mt_csvfile":"csvfile.csv"}
--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <csvfile.csv>
Content-Disposition: form-data;filename="csvfile.csv";name="csvfile.csv"
Content-Type: mimeType text/comma-separated-values;charset="uft-8"
Content-Description: csvfile.csv

"NUMBER","DESCRIPTION",
"00000000001","DESC 1",
"00000000002","DESC 2",
"00000000003","DESC 3",

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END--

All I care about is the CSV payload.

How can parse the response?
I am not asking how to parse a CSV file.

I am using httpClient:

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
    // need to get CSV values only
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2025-01-16 06:04:26

最后我使用了 Http-Multipart-Data-Parser。感谢 nazarn 给出了没有默认解决方案并使用包的提示。

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream stream = await response.Content.ReadAsStreamAsync())
{
    var parser = await MultipartFormDataParser.ParseAsync(stream);

    // CSV contents in Last file
    var file = parser.Files.Last();
    string filename = file.FileName;
    Stream data = file.Data;

    using (Stream fileStream = File.Create($@"C:\temp\{filename}"))
    {
        data.Seek(0, SeekOrigin.Begin);
        data.CopyTo(fileStream);
    }
}

In the end I used Http-Multipart-Data-Parser. Credits go to nazarn for giving the hint that there is no default solution and using a package.

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream stream = await response.Content.ReadAsStreamAsync())
{
    var parser = await MultipartFormDataParser.ParseAsync(stream);

    // CSV contents in Last file
    var file = parser.Files.Last();
    string filename = file.FileName;
    Stream data = file.Data;

    using (Stream fileStream = File.Create($@"C:\temp\{filename}"))
    {
        data.Seek(0, SeekOrigin.Begin);
        data.CopyTo(fileStream);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文