HTTPWebRequest和response如何显示分块响应的进度

时间:2018-12-18 04:37:56

标签: vb.net httpwebrequest httpwebresponse chunked

我当前正在使用HttpWebRequest和HttwebResponse。

一些响应被分块,并且响应中的内容长度为-1,这与预期的一样。

我的问题是我如何知道响应的大小以显示进度?

当前,我有这段代码,如果响应包含,则contentLength通常可以正常工作。

           If response.ContentEncoding = "gzip" Then
                    remoteStream = New IO.Compression.GZipStream(response.GetResponseStream(), IO.Compression.CompressionMode.Decompress)
                Else
                    remoteStream = response.GetResponseStream()
                End If

                localStream = IO.File.Create(localFilename)

                'Declare buffer as byte array
                Dim myBuffer As Byte()
                'Byte array initialization 
                ReDim myBuffer(1024)


                Dim bytesRead As Integer
                bytesRead = remoteStream.Read(myBuffer, 0, 1024)

                Do While (bytesRead > 0)
                    localStream.Write(myBuffer, 0, bytesRead)
                    bytesProcessed += bytesRead
                    Debug.Print(String.Concat("Download Progress: ", ((bytesProcessed / contentLength) * 100).ToString))

                    bytesRead = remoteStream.Read(myBuffer, 0, 1024)
                Loop
                localStream.Close()

0 个答案:

没有答案