c# HttpClient 解析来自 webservice 的响应
我正在调用一个超级旧的外部网络服务,它在响应中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我使用了 Http-Multipart-Data-Parser。感谢 nazarn 给出了没有默认解决方案并使用包的提示。
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.