简体   繁体   English

HttpWebRequest推迟写请求

[英]HttpWebRequest deferring write requests

I'm wanting to track the state of my upload request, something like : 我想跟踪我的上传请求的状态,例如:

  • Upload packet 上载封包
  • Update UI with progress 用进度更新UI
  • Upload packet 上载封包
  • Update UI with progress 用进度更新UI
  • Upload packet 上载封包
  • Update UI with progress 用进度更新UI
  • etc etc

So I tried the following code (gathered from examples found online). 因此,我尝试了以下代码(摘自在线示例)。

void SendData()
{
    byte[] bytes = Encoding.UTF8.GetBytes(GetContent());
    HttpWebRequest webRequest = WebRequest.Create(GetURL()) as HttpWebRequest;
    webRequest.Method = "PUT";
    webRequest.ContentType = @"application/x-www-form-urlencoded";
    webRequest.ContentLength = bytes.Length;

    // this doesn't seem to help
    webRequest.AllowWriteStreamBuffering = false;

    using (Stream reqStream = webRequest.GetRequestStream())
    {
        int bytesUploaded = 0;
        while (bytesUploaded != bytes.Length)
        {
            int packetSize = Math.Min(PACKETSIZE, bytes.Length - bytesUploaded);
            reqStream.Write(bytes /*buffer*/, 
                            bytesUploaded /*Offset*/, 
                            packetSize /*Count*/);

            // this doesn't seem to help
            reqStream.Flush();

            bytesUploaded += packetSize;
            UI.SetProgress(bytesUploaded / bytes.Length, 
                           true /*Dispatch?*/);
        }
    }

    WebResponse webResponse = webRequest.GetResponse(); //stalls here while it uploads
}

but it isn't working as expected. 但它没有按预期工作。 It isn't uploading the bytes after the flush, but instead running through the multiple iterations of the while loop, and then uploading at the GetResponse() stage. 它不是在刷新后上传字节,而是在while循环的多次迭代中运行,然后在GetResponse()阶段上传。 I know its not uploading within the while loop because no matter how large I set the byte array, the time taken to go through the loop barely changes; 我知道它不会在while循环中上载,因为无论我将字节数组设置为多大,遍历循环所花费的时间都几乎没有变化。 yet the time at the GetResponse() stage increase proportionately. 但是,GetResponse()阶段的时间成比例增加。

Resulting in the progress going from 0-100% in the blink of an eye, and then staying at 100% for the time it takes to actually send the data. 导致眨眼间进度从0%到100%,然后在实际发送数据所需的时间里保持在100%。

First, I can't find the definition of your PACKETSIZE. 首先,我找不到您的PACKETSIZE的定义。 Did you debug it and made sure that your bytes is longer than the packet size constant? 您是否调试了它并确保您的bytes长于数据包大小常量?

Second, I doubt you're using that loop correctly. 其次,我怀疑您是否正确使用了该循环。 When you're uploading , your GetRequestStream is updated at the end, and trying to flush it midway doesn't seem correct. 当您上传时,您的GetRequestStream会在最后更新,并且尝试在中途刷新它似乎不正确。 (Have a look at this answer for more details). (有关更多详细信息,请查看答案)。

Try this: break the array into 10 pieces. 试试这个:将数组分成10个部分。 Run a loop from 0-9, and in each send the corresponding array bit, wait for the response, flush/close the stream, then call the UI.SetProgress . 从0-9运行一个循环,并在每个循环中发送相应的数组位,等待响应,刷新/关闭流,然后调用UI.SetProgress Rinse and repeat. 冲洗并重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM