0

Before downloading a file from a link, I need to get some of its data (name, size, Content-type etc.)

            WebResponse response = null;
            using (token.Token.Register(() => client.Abort(), useSynchronizationContext: false))
            {
                response = await Task.Run(() => client.GetResponseAsync()).ConfigureAwait(true);
                token.Token.ThrowIfCancellationRequested();
            }

after I check the client type and get the necessary information. But there is a type of links for which I can’t get the data. When calling

response = await Task.Run (() => client.GetResponseAsync ())

error 404 is returned. What to do? I take an example of links with https://www.mp3juices.cc/

  • Hello, can you show a sample that can reproduce the problem? Or you can check the URL you are requesting during debugging, copy it to the browser, and open it to see if you can get the content. 404 usually means that the corresponding web resource does not exist, so you need to make sure that the URL you request is valid. – Richard Zhang Jul 13 '20 at 07:11
  • Hello, URL is valid(if copy URL in to the browser i see content – Анастасия Неминущая Jul 13 '20 at 07:25
  • Hello, can you provide a sample or complete code to reproduce the problem (please don't have private information), the current code lacks some key information, and temporarily cannot reproduce your problem. – Richard Zhang Jul 13 '20 at 07:43
  • https://drive.google.com/file/d/1QjktYlq0h3SYEVqKImkH3rsbFQod4tX7/view?usp=sharing – Анастасия Неминущая Jul 13 '20 at 08:01
  • Hello, I tested your code, it did not throw a 404 exception, I tested `https://www.mp3juices.cc/`, `https://www.microsoft.com` and other URLs, all Works fine. You can try to replace the network and re-test, and check whether the text you entered is abnormal. – Richard Zhang Jul 13 '20 at 08:12
  • i have tried(. In https://www.mp3juices.cc/ find "SIA" and try download Sia - The Greatest (second song) – Анастасия Неминущая Jul 13 '20 at 08:19

1 Answers1

0

The download address you requested does not apply to WebRequest processing, it does not point to a web page or file.

When accessing the URL, it is actually processed by the website processing program and returns the file. WebRequest directly accesses the URL and cannot get the returned data.

If you want to verify this, you can use BackgroundDownloader to directly download the file corresponding to the URL.

private StorageFile destinationFile;
private async void Button_Click(object sender, RoutedEventArgs e)
{
    Uri url = new Uri(Link.Text);

    destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
        "test.html", CreationCollisionOption.GenerateUniqueName);

    BackgroundDownloader downloader = new BackgroundDownloader();
    DownloadOperation download = downloader.CreateDownload(url, destinationFile);
    download.RangesDownloaded += DownloadHandle;
    await download.StartAsync();
}

private async void DownloadHandle(DownloadOperation sender, BackgroundTransferRangesDownloadedEventArgs args)
{
    string content = await FileIO.ReadTextAsync(destinationFile);
    Debug.WriteLine(content);
}

Thanks.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • second song has link, that redirect to another link and destinationFile is not a song, it is a html file – Анастасия Неминущая Jul 13 '20 at 10:24
  • Hello, the html file is directly obtained through the link, and the js script in the html file will be executed in the browser to download the file. But in C# `WebRequest` it will fail to do so. This is why we can download music files in the browser, but we can’t do the same in `WebRequest`. – Richard Zhang Jul 13 '20 at 11:24
  • I understand it, and download files by BackgroundDownloader class, but before download i need to check URL – Анастасия Неминущая Jul 13 '20 at 11:39
  • `BackgroundDownloader` is suitable for downloading URLs that directly lead to files. The web page you provide is not suitable for this. You can convert according to your needs. – Richard Zhang Jul 13 '20 at 11:55