Hack Forums
[.NET] Detect 404 When Downloading File In Loop? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Detect 404 When Downloading File In Loop? (/showthread.php?tid=5819591)



Detect 404 When Downloading File In Loop? - Haters Ganna Hate - Apr 2, 2018

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?


RE: Detect 404 When Downloading File In Loop? - VmX5 - Apr 2, 2018

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/visual-basic/language-reference/statements/try-catch-finally-statement



RE: Detect 404 When Downloading File In Loop? - Ninja - Apr 2, 2018

(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.


RE: Detect 404 When Downloading File In Loop? - Haters Ganna Hate - Apr 2, 2018

(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/visual-basic/language-reference/statements/try-catch-finally-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?


RE: Detect 404 When Downloading File In Loop? - gigajew - Apr 2, 2018

Something like this may work