0

I wrote below async method to download file stream from WebApi. But while awaiting result at networkStrm = await httpClient.GetStreamAsync(_downloadRestUrl);

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. The underlying connection was closed: An unexpected error occurred on a receive.

    async internal Task<Stream> DownloadAsync(string _downloadRestUrl)
    {
        Stream networkStrm = null;
        try
        {
            var baseUrl = GetBaseUrl(_downloadRestUrl);

            HttpClient httpClient = new HttpClient();
            httpClient.Timeout = TimeSpan.FromMinutes(5);
            httpClient.BaseAddress = baseUrl;
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));

            networkStrm = await httpClient.GetStreamAsync(_downloadRestUrl);
            networkStrm.Seek(0, SeekOrigin.Begin);
        }
        catch (Exception ex)
        {

        }
        return networkStrm;
    }
Abhijeet
  • 13,562
  • 26
  • 94
  • 175
  • Is the url working with a different client? Could any of the solutions in [this question](http://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host) work for you? – Moti Azu Jul 29 '14 at 07:33
  • @mot Url is fine. Same code if I write non-async version it works fine. – Abhijeet Jul 29 '14 at 07:37
  • Please edit your question and add info about how it works non-async, this really changes the kind of answers you are going to get. – Moti Azu Jul 29 '14 at 07:59
  • 1
    Could you use Wireshark or Fiddler to see if there is a difference between the successful and unsuccessful requests? – Moti Azu Jul 29 '14 at 08:02

0 Answers0