Download file with Xamarin

后端 未结 2 1361
北海茫月
北海茫月 2021-01-20 23:47

I\'m trying to download file with Xamarin, but receive an error massage:

An exception occurred during a WebClient request. I thing that the problem is wit

相关标签:
2条回答
  • 2021-01-21 00:09
    var httpClientHandler = new HttpClientHandler
    {
        AllowAutoRedirect = false,
        ....
    };
    
    var httpClient = new System.Net.Http.HttpClient(httpClientHandler)
    {
        MaxResponseContentBufferSize = 5000000,
        ...
    };
    
    var uri = new Uri("http://x.com");
    using (var response = await httpClient.GetAsync(uri))
    {
      if (!response.IsSuccessStatusCode)
           throw new HttpRequestException($"URL {uri} not loaded. {response.StatusCode}");
    
        var str = await response.Content.ReadAsStringAsync();
        // ...
    }
    
    0 讨论(0)
  • 2021-01-21 00:22

    You only created a folder, but didn't create a file for download file, you can just modify your code webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder); for example like this:

    webClient.DownloadFileAsync(new Uri("http://www.dada-data.net/uploads/image/hausmann_abcd.jpg"), folder + "/abc.jpg"); 
    
    0 讨论(0)
提交回复
热议问题