简体   繁体   English

如何在 ASP.Net MVC 应用程序中使用、读取和解压缩第三方 API 响应

[英]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.

相关问题 如何在ASP.Net MVC应用程序的构建期间合并第三方api调用 - How to incorporate a third party api call during build time in ASP.Net MVC Application 如何从MVC 6.0和asp net core应用程序中的第三方rest API获取数据 - How to get data from third party rest API in MVC 6.0 and asp net core application ASP.NET 核心 Web API - 如何使用第 3 方 API 基于条件 - ASP.NET Core Web API - How to Consume 3rd party API Based on Condition ASP.NET Core Web API - 如何使用 HttpClient 使用 3rd 方 API - ASP.NET Core Web API - How to Consume 3rd party API using HttpClient 找不到第三方DLL C#ASP.NET MVC - Third party DLL not found C# ASP.NET MVC 使用第三方托管发布 ASP.NET 应用程序 - Publishing ASP.NET application using third party hosting ASP.NET Core Web API - 如何将指定字段作为有效负载传递给第三方 API - ASP.NET Core Web API - How to pass specified fields as payload to third party API 如何在 ASP.NET Core 6 Web API 中从第三方 API 获取数据 - How to fetch data from a third party API in ASP.NET Core 6 Web API ASP.NET 核心 - 如何在发送 Email 之前检查来自给定第三方 API 的字段是否存在 - ASP.NET Core - How to check if a field exists from a given third party API before sending Email 如何从第三方访问令牌asp.net Web api2获取用户电子邮件? - How to get User Email from third party access token asp.net web api2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM