0

I want to download video by URL and save it into my desk (windows application) using C#

That's what I have written:

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)))
{
psfinaki
  • 1,814
  • 15
  • 29
mariam
  • 9
  • 6
  • ` and ' are different characters. Only one works for code formatting. – Joel Coehoorn Oct 05 '20 at 16:10
  • You shouldn’t use WebRequest. It’s been obsolete for almost a decade now and the documentation states you should use HttpClient instead. If you use HttpClient you can just call ReadAsStreamAsync on the response and copy it to a FileStream. – ckuri Oct 05 '20 at 16:14
  • 1
    Does this answer your question? [How to download a file from a URL in C#?](https://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url-in-c) – Heretic Monkey Oct 05 '20 at 16:16

1 Answers1

1

You may be making this much harder than it needs to be:

using (var wc = new WebClient())
{
   wc.DownloadFile(uri, "path to download to");
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794