How to catch 404 Not Found

10-15-2012, 11:15 AM
I am really stuck on this one. I have been looking and trying things I have been finding on Google but nothing seems to be working. My code below works perfectly unless of course there is no Setup.exe file found. Then I get "The remote server returned an error: (404) Not found.)

I can not seem to figure out a way to trap this so I can show my own custom message such as "No update found".

If Me.DownLoadFile("", "Setup.exe") Then
Process.Start(Application.StartupPath & "/Update.exe")
End If

I thought this might work but I still get the same thing...

Me.DownLoadFile("", "Setup.exe")
Process.Start(Application.StartupPath & "/Update.exe")
Catch ex As WebException

Could anyone show me how I can try this in VB.NET 2010 please?

10-15-2012, 02:34 PM
I am really stuck on this one. I have been looking and trying things I have been finding on Google but nothing seems to be working..
I'm not an web programming expert, but (since my Google is working),
I suspect this is what is referred to as one of those "vexing exceptions (".

As you may have guessed, the structured exception handling you are using (Try..Catch) is not quite right..

One way might be to use a System.Net.WebException like:
Public Function GetResponse(ByRef URL As String) As HttpWebResponse

'Create a web request
Dim m_Req As HttpWebRequest = HttpWebRequest.Create(URL)

'Get a web response
Return m_Req.GetResponse()
Catch ex As System.Net.WebException
If ex.Status = WebExceptionStatus.ProtocolError Then
Return ex.Response
End If
End Try
Return Nothing
End Function

..or another (more common) example (usually used with web pages) of a Try.catch exception handling for 404 might be:
Dim wc As New System.Net.WebClient()
wc.DownloadFile("", "C:\temp\somefilethatdoesntexist.xls")
Catch ex As System.Net.WebException
Dim response As System.Net.HttpWebResponse = ex.Response
Select Case response.StatusCode
Case System.Net.HttpStatusCode.NotFound, System.Net.HttpStatusCode.Unauthorized
' Do something with not founds or unauthorized
Console.WriteLine("Ignoring : " & ex.ToString())
Case Else
Throw ex
End Select
End Try

If you are intent on using the Network.DownloadFile method ( you might want to check out the "roll your own" link on this StackOverFlow page:
"Politely checking to see if a file exists before downloading ("

Note: In terms of taking the time to design a full set of except handling code routines
you might want to consider handling all of the different types of status codes ( you might conceivably receive (not just 404).

Another download file error I have run into is when the path or file name is too long.
Structured exception handling and careful use of the WebException class ( and the FileWebRequest Class ( (in combination with the webException.Response.GetResponseStream), can be used to handle this as well.

In such cases it might also be handy to use the the LongPathFile class from the BCL project ( and focus on using DownloadData ( instead of DownloadFile.

For others who happen upon this thread, it might be worthwhile to mention
that the IAsyncResult Interface ( can be used to read the stream returned by the WebResponse.GetResponseStream method.
For code and details see this MSDN Making Asynchronous Requests page (

I also found some code for a "Public Function DownloadFileWithProgress"
on this social.msdn thread page (, which notes that the code works perfectly except when using:
non_ASCII characters in the names of the folders and files on the web server -- if I use non-ASCII character I get error 404 'not found'.
The workaround noted in that thread was:
I have managed to find and use HttpServerUtility.UrlEncode and HttpServerUtility.UrlDecode.
They are brilliant! Exactly what I needed.
I can apply them immediately because they will replace the encoding system I invented and encode all types of character French, Chinese etc.

Lastly..the thing that occurred to me is:
"If you are downloading files why don't you just use FTP with VB.Net code?

There are many articles on this so I just throw out one from the Microsoft support site:
"How To Write Pluggable Protocol to Support FTP in Managed Classes by Using Visual Basic .NET ("

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum