【问题标题】:Download video from URL and save it to the hard drive从 URL 下载视频并将其保存到硬盘
【发布时间】:2020-10-06 03:59:35
【问题描述】:

我想通过 URL 下载视频并使用 C# 将其保存到我的桌面(Windows 应用程序)

这就是我写的:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if ((response.StatusCode == HttpStatusCode.OK ||
     response.StatusCode == HttpStatusCode.Moved ||
     response.StatusCode == HttpStatusCode.Redirect) &&
     (response.ContentType.StartsWith("video", StringComparison.OrdinalIgnoreCase) || response.ContentType.EndsWith("octet-stream", StringComparison.OrdinalIgnoreCase)))
{

【问题讨论】:

  • ` 和 ' 是不同的字符。只有一种适用于代码格式化。
  • 你不应该使用 WebRequest。它已经过时了将近十年,文档指出您应该改用 HttpClient。如果您使用 HttpClient,您可以在响应上调用 ReadAsStreamAsync 并将其复制到 FileStream。
  • 这能回答你的问题吗? How to download a file from a URL in C#?

标签: c#


【解决方案1】:

你可能比它需要的更难:

using (var wc = new WebClient())
{
   wc.DownloadFile(uri, "path to download to");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 2016-05-30
    • 2015-09-25
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多