Windows UWP c#HttpClient发布和进度
因此,我需要在我的应用程序上安装一个progressbar,我复制了微软的开源示例(抱歉,不抱歉),它的工作非常出色,除非您必须包含POST数据(或者我只是个白痴,无法理解它) 工作代码:Windows UWP c#HttpClient发布和进度,c#,php,uwp,C#,Php,Uwp,因此,我需要在我的应用程序上安装一个progressbar,我复制了微软的开源示例(抱歉,不抱歉),它的工作非常出色,除非您必须包含POST数据(或者我只是个白痴,无法理解它) 工作代码: const uint streamLength = 100000; HttpStreamContent streamContent = new HttpStreamContent(new SlowInputStream(streamLength)); IProgress<Http
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);
我将如何将我的formContent(或者如果你想用另一种方式,也可以)放入streamContent,因为如果没有streamContent,进度就不起作用
编辑1:
所以,它起作用了,现在进度不再起作用了。这是我的进度函数
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;
}
下面是它在调试中显示的内容:
Stage: ReceivingContent
Retires: 0
Bytes Sent: 180
Bytes Received: 6757607
Total Bytes To Send: 180
Total Bytes To Receive: unknown
Progress: 50
尽管我看到收到的字节数在增加,但它始终是未知的。所以我的答案几乎是空的,我只需要在php脚本上设置内容长度来获得代码的大小,它显示的是未知的。我仍然不知道它以前是如何得到它的。你可以使用StringContent作为文本。@kiewic我使用了多内容它似乎应该可以工作,但不能。我的PHP脚本无法识别POST数据,即使通过fiddler它看起来是正确的。我尝试了没有流的多部分,只有POST数据,但仍然无法工作。
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;
}
Stage: ReceivingContent
Retires: 0
Bytes Sent: 180
Bytes Received: 6757607
Total Bytes To Send: 180
Total Bytes To Receive: unknown
Progress: 50