[英]How to consume, read and unzip a third party API response in ASP.Net MVC application
I am trying to consume a third party API whose URL looks like this:我正在尝试使用其 URL 如下所示的第三方 API:
https://api.crowdin.com/api/project/{PROJECT_NAME}/download/all.zip?key={MY_KEY}
This api returns a zip file as "all.zip" as response.这个 api 返回一个 zip 文件作为“all.zip”作为响应。
When I go to browser and make this request I get a all.zip file downloaded.当我转到浏览器并提出此请求时,我下载了一个 all.zip 文件。 Now I want to write C# code to get this result.
现在我想编写 C# 代码来得到这个结果。 Below is my attempt:
以下是我的尝试:
public async Task<ActionResult> Index()
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.crowdin.com/");
HttpResponseMessage response = await client.GetAsync("api/project/{MY_PROJECT}/download/all.zip?key={MY_KEY}");
// WHAT TO WRITE HERE
return View();
}
Question 1: I got the successful response and content type is application/zip
, but now I don't know how to read this response.问题 1:我收到了成功的回复,内容类型是
application/zip
,但现在我不知道如何阅读此回复。
Question 2: I want the response to unzipped and saved to a folder.问题 2:我希望将响应解压缩并保存到文件夹中。
PS: The response .zip file is a collection of .resx File. PS:响应.zip文件是.resx 文件的集合。
This is mostly from memory so I haven't tested the code.这主要来自记忆,所以我没有测试代码。 It should get you pretty close to what you're looking for:
它应该让你非常接近你正在寻找的东西:
Saving the response to file:将响应保存到文件:
var response = httpClient.GetAsync("api/project/{MY_PROJECT}/download/all.zip?key={MY_KEY}");
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fs = new FileStream(filename) {
await stream.CopyToAsync(fs);
}
Unzipping the file (you could also do this in memory)解压缩文件(您也可以在内存中执行此操作)
System.IO.Compression.ZipFile.ExtractToDirectory(filename, extractPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.