Httpclient Content-Length始终为0字节

问题描述

有人可以帮我吗,获取视频文件内容长度仍然有问题。我正在尝试下载流视频文件。但我从响应中只能得到0byte。

这是我当前的代码。我不知道如何正确设置内容类型。你能帮我吗?

public async Task StartDownload(CancellationToken token)
        {
            Uri uri = new Uri(DownloadUrl,UriKind.Absolute);
            SetConnection(uri);
            using (var request = new HttpRequestMessage(HttpMethod.Get,uri)) {
                request.RequestUri = uri;
                request.Headers.Referrer = uri;
                request.Headers.UserAgent.ParseAdd(UserAgent);
                using (var response = await client.SendAsync(request,HttpCompletionoption.ResponseHeadersRead,token)) {
                    token.ThrowIfCancellationRequested();
                    await DownloadFileFromHttpResponseMessage(response,token);
                }
            }
        }
        private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response,CancellationToken token)
        {
            response.EnsureSuccessstatusCode();
            using (var contentStream = await response.Content.ReadAsstreamAsync())
            {
                using (var content = new StreamContent(contentStream))
                {
                    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
                    long? totalBytes = response.Content.Headers.ContentLength;
                    Console.WriteLine(totalBytes.GetValueOrDefault());

                    long totalBytesRead = 0;
                    long readCount = 0;
                    byte[] buffer = new byte[4096];
                    bool isMoretoRead = true;

                    using (var fileStream = new FileStream(OutputFilePath,FileMode.Create,FileAccess.Write,FileShare.None))
                    {
                        do
                        {
                            token.ThrowIfCancellationRequested();
                            int bytesRead = await contentStream.ReadAsync(buffer,buffer.Length,token);
                            if (bytesRead == 0)
                            {
                                isMoretoRead = false;
                                TriggerProgressChanged(totalBytes,totalBytesRead);
                                continue;
                            }
                            await fileStream.WriteAsync(buffer,bytesRead);

                            totalBytesRead += bytesRead;
                            readCount += 1;

                            if (readCount % 10 == 0)
                            {
                                TriggerProgressChanged(totalBytes,totalBytesRead);
                            }
                        }
                        while (isMoretoRead);
                    }
                    TriggerProgressChanged(totalBytes,totalBytesRead);
                }
            }
        }
        private void TriggerProgressChanged(long? totalDownloadSize,long totalBytesRead)
        {
            if (ProgressChanged == null) { return; }

            double? progresspercentage = null;
            if (totalDownloadSize.HasValue) {
                progresspercentage = Math.Round((double)((double)totalBytesRead / totalDownloadSize * 100),2);
           }
            ProgressChanged(totalDownloadSize,totalBytesRead,progresspercentage);
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)