So I need a progressbar on my application and I copied Microsoft's open source example (sorry not sorry), and it works amazing, except if you have to include POST data (or I'm just an idiot and can't figure it out.
Working code:
const uint streamLength = 100000;
HttpStreamContent streamContent = new HttpStreamContent(new SlowInputStream(streamLength));
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ProgressHandler);
response = await httpClient.PostAsync(new Uri(settings.url + "/script.php"), streamContent).AsTask(cts.Token, progress);
That gives me my progress and everything, now I need to add 4 post values, before I had them in a dictionary like so:
var values = new Dictionary<string, string>
{
{ "1", "1" },
{ "2", "2" },
{ "3", "3" },
{ "4", "4" }
};
Then I encoded them
HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(values);
How would I get my formContent (or if you want to do it another way, that works too) into the streamContent, because it looks like without the streamContent, that the progress does not work.
Edit 1:
So, it works, now the progress does not work anymore. Here Is my progress function
private void ProgressHandler(HttpProgress progress)
{
Debug.WriteLine("Stage: " + progress.Stage.ToString());
Debug.WriteLine("Retires: " + progress.Retries.ToString(CultureInfo.InvariantCulture));
Debug.WriteLine("Bytes Sent: " + progress.BytesSent.ToString(CultureInfo.InvariantCulture));
Debug.WriteLine("Bytes Received: " + progress.BytesReceived.ToString(CultureInfo.InvariantCulture));
ulong totalBytesToSend = 0;
if (progress.TotalBytesToSend.HasValue)
{
totalBytesToSend = progress.TotalBytesToSend.Value;
Debug.WriteLine("Total Bytes To Send: " + totalBytesToSend.ToString(CultureInfo.InvariantCulture));
}
else
{
Debug.WriteLine("Total Bytes To Send: " + "unknown");
}
ulong totalBytesToReceive = 0;
if (progress.TotalBytesToReceive.HasValue)
{
totalBytesToReceive = progress.TotalBytesToReceive.Value;
Debug.WriteLine("Total Bytes To Receive: " + totalBytesToReceive.ToString(CultureInfo.InvariantCulture));
}
else
{
Debug.WriteLine("Total Bytes To Receive: " + "unknown");
}
double requestProgress = 0;
if (progress.Stage == HttpProgressStage.SendingContent && totalBytesToSend > 0)
{
requestProgress = progress.BytesSent * 50 / totalBytesToSend;
}
else if (progress.Stage == HttpProgressStage.ReceivingContent)
{
// Start with 50 percent, request content was already sent.
requestProgress += 50;
if (totalBytesToReceive > 0)
{
requestProgress += progress.BytesReceived * 50 / totalBytesToReceive;
}
}
else
{
return;
}
Debug.WriteLine("Progress: " + requestProgress);
snap_progress.Value = requestProgress;
}
And here is what it shows in debug:
Stage: ReceivingContent
Retires: 0
Bytes Sent: 180
Bytes Received: 6757607
Total Bytes To Send: 180
Total Bytes To Receive: unknown
Progress: 50
It is at unknown the entire time even though I see Bytes Received increasing.