When receiving information about a file (via the link), an error 404 occurs UWP

144 Views Asked by At

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/

1

There are 1 best solutions below

4
On

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.