How to get content from file from this URL?

前端 未结 4 2025
北海茫月
北海茫月 2020-12-08 14:44

I have this URL: URL from Google

When open link in new tab, the browser force me download it. After download, I get a text file named \"s\". But I want use C# acces

4条回答
  •  失恋的感觉
    2020-12-08 15:18

    For asp.net core / .Net 5+, you should inject HttpClient in your service. You should not manually create a new instance.

    public class MySerivice {
       private readonly HttpClient _httpClient;
       public MyService(HttpClient httpClient) {
           _httpClient = httpClient;
       }
       
       public async Task Foo() {
           var myString = await _httpClient.GetStringAsync("https://my-url/file.txt");
       }
    }
    

    Injecting HttpClient will use IHttpClientFactory behind the scenes. Docs: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

提交回复
热议问题