Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Detect 404 When Downloading File In Loop?

So I want to download a bunch of mp3 files from this website, the website's URL structure is like this:
Code
http://thiswebsite.com/music/*.mp3
where the * is a number between 0 and 1000

Now, there is not 1000 songs, there could be songs that are from 100 to 200 but not 200 to 300. I discovered this site is using nginx and returns a proper 404 if a file does not exists, like this:

[Image: B6IdOLu.png]

[Image: UF9hStX.png]

I have my code structured like this:
Code
Private Sub DownloadMusicFile()
       'Downloading Music
       Dim MusicNumber As Integer = 0
       Do Until MusicNumber = 1000
           My.Computer.Network.DownloadFile("http://thiswebsite.com/music/" + MusicNumber + ".mp3",
                                            My.Computer.FileSystem.CurrentDirectory + "/music/" + MusicNumber + "/")
       Loop
   End Sub

The thing is, I want my program to create a folder and then store the mp3 in it, so a new folder for each mp3 file but I want to check to see if the file actually exists before I go ahead and create the folder. How would I go on to do that?
Look into a try statement,

Code
Try
  DownloadFile("unknown url")
Catch exception as filenotfound(404 error)
  'Code to run if ^ error
End try

https://docs.microsoft.com/en-us/dotnet/...-statement
(Apr 2, 2018 09:50 AM)Haters Ganna Hate Wrote:  so a new folder for each mp3 file but I want to check to see if the file actually exists before I go ahead and create the folder. How would I go on to do that?

Directory.CreateDirectory first checks if the directory exists, and if it does not, creates the directory. If you want to check only if the directory exists then use Directory.Exists.

EDIT: Also, in your code you never increase the MusicNumber integer so the loop is endless. And instead of a Do Until integer = value I suggest you use a for loop.

EDIT2: A 404 results in a WebException. Use a Try-Catch block to catch the WebException. The WebException has a Response property. If you cast that WebResponse to a HttpWebResponse you can check the StatusCode if it is 404 or something else.
(Apr 2, 2018 09:52 AM)VmX5 Wrote:  Look into a try statement,

Code
Try
  DownloadFile("unknown url")
Catch exception as filenotfound(404 error)
  'Code to run if ^ error
End try

https://docs.microsoft.com/en-us/dotnet/...-statement

(Apr 2, 2018 10:21 AM)Ninja Wrote:  Directory.CreateDirectory first checks if the directory exists, and if it does not, creates the directory. If you want to check only if the directory exists then use Directory.Exists.

EDIT: Also, in your code you never increase the MusicNumber integer so the loop is endless. And instead of a Do Until integer = value I suggest you use a for loop.

EDIT2: A 404 results in a WebException. Use a Try-Catch block to catch the WebException. The WebException has a Response property. If you cast that WebResponse to a HttpWebResponse you can check the StatusCode if it is 404 or something else.

Hey, So I looked at the recommendations you guys posted here and then later modified it to work with BackgroundWorkers. This is what I have now: https://pastebin.com/R27U6ZJG

The thing is, with 404 pages, it doesnt skip to the next file, it downloads the 404 page as a .mp3 which means the WebException is not being caught. What can I do to fix this?
Something like this may work
[Image: Syr3DlV.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by lasermatApr 19, 2022 12:14 PM 2 251 Apr 19, 2022 12:14 PM
Last Post: _0xC_
[C] by ConcentratedNagaJan 18, 2021 10:51 PM 0 154 Jan 18, 2021 10:51 PM
Last Post: ConcentratedNaga
[C#] by notahackermanApr 22, 2020 06:54 AM 21 358 Apr 22, 2020 06:54 AM
Last Post: notahackerman