LuisBerga
LuisBerga

Reputation: 67

httpclient GetAsync doesn't work for large content

I am trying to download the string content from an endpoint in question using xamarin and http requests. The endpoint I'm supporting with python flask, as well as the content of the page, which is similar to this:

[{"name":"test1","timestamp":12312,"type":"ultrasonic","value":65535}, 
{"name":"test2","timestamp":3123123,"type":"ultrasonic","value":65535}...]

When there are not many lines in the content of the page, I can access the content and save it to a file inside the android device. When the content is very long, with many lines, the application simply closes.

My code is as follows:

public async Task<string> GetNews()
{
    List<string> valores = new List<string>();    
    var response = await client.GetAsync(get_url);    
    var responseData = await response.Content.ReadAsStringAsync();    
    string nome = DateTime.Now.ToString("HH:mm:ss");    
    //creating the subsequent file
    string filename = System.IO.Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.Personal),
        "Sensor File - " + nome + ".json");
    //inserting the content of the GET into the generated text file
    System.IO.File.WriteAllText(filename, responseData);
    return null;
}
public async void Download_Clicked(object sender, EventArgs e)
{
    try
    {
        await GetNews(); 
        //informing in the first label the directory of the saved file
        lb.Text = "File saved successfully!";
    }
    catch
    {
        lb.Text = "Connection not possible, enter the correct URL.";
    }
}

I did add .ConfigureAwait(false) to each await line, but it didn't work... What am I doing wrong?

Thank you!

Upvotes: 2

Views: 1011

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10958

When you used HttpClient to download large amounts of data (50 megabytes or more), then the app should stream those downloads and not use the default buffering. If the default buffering is used the client memory usage will get very large, potentially resulting in substantially reduced performance.

You could try to get the response body and load into memory.

using (var response = await client.GetAsync(get_url))
    using (Stream streamToReadFrom =
        await response.Content.ReadAsStreamAsync())
    {
            ...........
            //string fileToWriteTo = Path.GetTempFileName();
            //using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
            //{
            //    await streamToReadFrom.CopyToAsync(streamToWriteTo);
            //}
    }

Upvotes: 2

Related Questions