#StackBounty: #c# #.net #rest #httpwebrequest #webclient Webclient + UploadStringAsync + Events: UploadProgressChanged / DownloadProgre…

Bounty: 50

I need to communicate with a REST service.

For some reasons, I have to use a POST request to obtain JSON data from that service. (I think it should use a GET request but this is not under my control and I can’t change it…)

The response for one request is around 25-30 MB.
Since the response is big I decided to use the WebClient and the UploadStringAsync method.

private string jsonResult = "";

void test()
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + passowrd));  
    using (System.Net.WebClient client = new System.Net.WebClient())
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressHandler);
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedHandler);                
        client.Headers.Add("Authorization", "Basic " + credentials);
        client.Encoding = System.Text.Encoding.UTF8;
        Uri uri = new Uri(https://demoapi.org/stuff/prod/0);

        client.UploadStringAsync(uri, "POST", "");

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);

private static void client_UploadProgressHandler(object sender, UploadProgressChangedEventArgs e)
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete, {3} of {4} bytes. {5} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);

private void client_UploadStringCompletedHandler(Object sender, UploadStringCompletedEventArgs e)
    jsonResult = e.Result;

With the code above, the console shows many times the message UPLOAD_PROGRESS! and once the message UPLOAD_COMPLETED! However, I don’t see the message DOWNLOAD_PROGRESS!


Here are my questions:

1) Can I (really) subscribe to the “UploadProgressChanged” Event with the webclient.UploadStringAsync method ?

The Microsoft documentation does not says that the UploadProgressChanged is available for UploadStringAsync but anyhow it seems to be working with the code above.



I also looked at the Microsoft referencesource but I don’t fully understand it.
The delegate ‘UploadProgressChanged’ does not seem to be present in the UploadStringAsync method.


So, I’m wondering if it’s correct to use “UploadProgressChanged” with the webclient.UploadStringAsync.

2) Can I subscribe to the “DownloadProgressChanged” event with the webclient.UploadStringAsync method ?

3) As my understanding, the webclient is using a WebRequest and WebResponse under the hood. Is it possible to get the progress of both requests ?

I’m asking this because sometimes the body of the request is big and sometimes the reponse is big.

4) If the DownloadProgressChangedEvent is not available, then
how can I report the progress of the WebResponse part of the “UploadStringAsync” POST request ?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.