当源未实现 ContentLength 时,从另一个 Web 文件获取 HttpWebRequest 上传的响应长度以流式传输到上传中?

发布于 2024-08-14 07:17:26 字数 688 浏览 7 评论 0原文

背景 - 我正在尝试使用 C# 中的 HttpWebRequest/HttpWebResponse 将现有网页流式传输到单独的 Web 应用程序。我注意到的一个问题是,我试图使用文件下载的内容长度来设置文件上传请求的内容长度,但是问题似乎是当源网页位于 HttpWebResponse 不支持的网络服务器上时提供内容长度。

HttpWebRequest downloadRequest = WebRequest.Create(new Uri("downloaduri")) as HttpWebRequest;
 using (HttpWebResponse downloadResponse = downloadRequest.GetResponse() as HttpWebResponse)
 {
   var uploadRequest = (HttpWebRequest) WebRequest.Create(new Uri("uripath"));
   uploadRequest.Method = "POST";
   uploadRequest.ContentLength = downloadResponse.ContentLength;  // ####

问题:我如何更新此方法来满足这种情况(当下载响应没有设置内容长度时)。也许会以某种方式使用 MemoryStream 吗?任何示例代码将不胜感激。

Background - I'm trying to stream an existing webpage to a separate web application, using HttpWebRequest/HttpWebResponse in C#. One issue I'm striking is that I'm trying to set the file upload request content-length using the file download's content-length, HOWEVER the issue seems to be when the source webpage is on a webserver for which the HttpWebResponse doesn't provide a content length.

HttpWebRequest downloadRequest = WebRequest.Create(new Uri("downloaduri")) as HttpWebRequest;
 using (HttpWebResponse downloadResponse = downloadRequest.GetResponse() as HttpWebResponse)
 {
   var uploadRequest = (HttpWebRequest) WebRequest.Create(new Uri("uripath"));
   uploadRequest.Method = "POST";
   uploadRequest.ContentLength = downloadResponse.ContentLength;  // ####

QUESTION: How could I update this approach to cater for this case (when the download response doesn't have a content-length set). Would it be to somehow use a MemoryStream perhaps? Any sample code would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-08-21 07:17:26

如果您很乐意从其他网络服务器完全下载响应,那确实会让生活变得更轻松。当您从第一个 Web 服务器获取数据时,只需重复写入 MemoryStream,然后您就知道为第二个请求设置的长度,并且可以轻松写入数据(特别是 MemoryStream 有一个 WriteTo 方法写入将其内容传输到另一个流)。

这样做的缺点是,如果文件很大,您将占用大量内存。在您的情况下这可能是一个问题吗?替代方案包括:

  • 写入文件而不是使用MemoryStream。当然,之后您需要清理文件 - 您基本上是在使用文件系统作为更大的内存:)
  • 使用分块传输编码来“读取一个块,写入一个块”;这可能很难做到正确 - 这肯定不是我以前做过的事情。

If you're happy to download the response from the other web server completely, that would indeed make life easier. Just repeatedly write into a MemoryStream as you fetch from the first web server, then you know the length to set for the second request and you can write the data in easily (especially as MemoryStream has a WriteTo method to write its contents to another stream).

The downside of this is that you'll take a lot of memory if it's a large file. Is that likely to be a problem in your situation? Alternatives include:

  • Writing to a file instead of using a MemoryStream. You'll need to clean up the file afterwards, of course - you're basically using the file system as bigger memory :)
  • Using a chunked transfer encoding to "read a chunk, write a chunk"; this may be fiddly to get right - it's certainly not something I've done before.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文