How to catch 404 Not Found

vb2008
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("http://isoncall.grhq.XXX.com/OperatorAssistUpdates/Setup.exe", "Setup.exe") Then
Process.Start(Application.StartupPath & "/Update.exe")
Application.Exit()
End If



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

Try
Me.DownLoadFile("http://isoncall.grhq.XXX.com/OperatorAssistUpdates/Setup.exe", "Setup.exe")
Process.Start(Application.StartupPath & "/Update.exe")
Application.Exit()
Catch ex As WebException



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

hDC_0
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 (http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx)".

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
Try
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:
Try
Dim wc As New System.Net.WebClient()
wc.DownloadFile("http://www.google.com/somefilethatdoesntexist.txt", "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
Console.WriteLine(ex.ToString())
Throw ex
End Select
End Try

If you are intent on using the Network.DownloadFile method (http://msdn.microsoft.com/en-us/library/ms127877%28v=vs.100%29.aspx) 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 (http://stackoverflow.com/questions/5966778/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 (http://msdn.microsoft.com/en-us/library/aa384325%28VS.85%29.aspx) 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 (http://msdn.microsoft.com/en-us/library/system.net.webexception%28v=vs.100%29.aspx) and the FileWebRequest Class (http://msdn.microsoft.com/en-us/library/system.net.filewebrequest%28v=vs.100%29.aspx) (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 (http://bcl.codeplex.com/wikipage?title=Long%20Path&referringTitle=Home) and focus on using DownloadData (http://msdn.microsoft.com/en-us/library/xz398a3f%28v=vs.100%29.aspx) instead of DownloadFile.

For others who happen upon this thread, it might be worthwhile to mention
that the IAsyncResult Interface (http://msdn.microsoft.com/en-us/library/system.iasyncresult%28v=vs.100%29.aspx) can be used to read the stream returned by the WebResponse.GetResponseStream method.
For code and details see this MSDN Making Asynchronous Requests page (http://msdn.microsoft.com/en-us/library/86wf6409%28v=vs.100%29.aspx).

I also found some code for a "Public Function DownloadFileWithProgress"
on this social.msdn thread page (http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/5e228dcf-2343-4ae5-98ca-335a5ecee1fc), 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 (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B812404)"

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum