dsy6639 2016-01-19 04:56
浏览 101

Windows UWP c#HttpClient POST和进度

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.

  • 写回答

1条回答 默认 最新

  • douchuoliu4422 2016-01-19 06:15
    关注

    So my answer is null pretty much, I just needed to set the Content-Length on my php script for the code to get the size, it was displaying unknown. I still don't know how it got it before.

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?