Skip to Content

Tracking progress of GET requests

In Windows SAP Mobile Platform SDK (hereinafter referred to as “SMP SDK”) SP09, several new networking features were introduced.  This was possible because Windows SDK now uses Windows.Web.Http.HttpClient internally. Prior to SP09, Windows SDK used System.Net.Http.HttpClient internally.  The new networking library provides the ability to track progress of GET requests (how much data was downloaded).

Note:  Due to this change, the public API of the networking component has changed for Windows Store apps.  Please refer to the blog by Zoltan Farkas on changes required to migrate an existing Windows application from SMP SDK SP08 (or lower) to SMP SDK SP09 (and higher).

The SAP.Net.Http.HttpClient has 8 overloaded SendAsync methods.  These overloaded methods are listed below.  The last 2 overloaded methods allow us to track the progress of a GET request – amount of data that has been downloaded.

public virtual Task<HttpResponseMessage> SendAsync(Func<HttpRequestMessage> requestGenerator);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<Task<HttpRequestMessage>> requestGenerator);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<HttpRequestMessage> requestGenerator, HttpCompletionOption completionOption);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<Task<HttpRequestMessage>> requestGenerator, HttpCompletionOption completionOption);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<HttpRequestMessage> requestGenerator, HttpCompletionOption completionOption, CancellationToken cancellationToken);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<Task<HttpRequestMessage>> requestGenerator, HttpCompletionOption completionOption, CancellationToken cancellationToken);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<HttpRequestMessage> requestGenerator, HttpCompletionOption completionOption, CancellationToken cancellationToken, HttpProgressDelegate progressDelegate);

       

public virtual Task<HttpResponseMessage> SendAsync(Func<Task<HttpRequestMessage>> requestGenerator, HttpCompletionOption completionOption, CancellationToken cancellationToken, HttpProgressDelegate progressDelegate);

In our example, we will use the following overloaded SendAsync method to track the progress of a GET request.

public virtual Task<HttpResponseMessage> SendAsync(Func<HttpRequestMessage> requestGenerator, HttpCompletionOption completionOption, CancellationToken cancellationToken, HttpProgressDelegate progressDelegate);

Tracking progress of the GET request is fairly easy to implement.  All that is required is an HttpProgressDelegate that needs to be passed into the SendAsync method.  The following code snippet allows us to track the progress of a GET request.  A label is updated as data is downloaded as part of the GET request.

public object cancellationTokenSource = null;

public string message = string.Empty;

public async Task SendGetRequestWithProgress()

{

   lblProgress.Text = textBox1.Text = message = String.Empty;

   try

   {

      using (var cts = new CancellationTokenSource())

      {

         using (var client = new SAP.Net.Http.HttpClient())

         {

            var responseMessage = await client.SendAsync(() => new HttpRequestMessage(

   HttpMethod.Get, new Uri(http://127.0.0.1/Northwind/NorthwindService.svc/Products)), HttpCompletionOption.ResponseContentRead, cts.Token,

  async (s, progress) => {

  await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.lblProgress.Text = String.Format(“{0} KB of {1} KB transferred…”, progress.BytesTransferred / 1024, progress.TotalBytesToTransfer / 1024); });

      });

            message = await responseMessage.Content.ReadAsStringAsync();

         }

      }

   }

   catch (Exception ex)

   {

      // handle the exception

   }

   if (!String.IsNullOrEmpty(message))

   {

      textBox1.Text = message.Substring(0, 100) + “…”;

   }

   cancellationTokenSource = null;

}

Please find attached the source code for a sample Windows application that tracks the progress of a GET request. 

In the next blog, I will talk about how we can add custom handlers / filters to the handler / filter chain. 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply