VB 2010 Download file and if 404 happen ignore error - vb.net

I'm beginner on VB 2010
and I wanna write an app that can downloads file
My question is:
If it gives me 404 or 403 or whatever, I wanna let the app ignore that message, instead of a webexception error
Note: I've already know how to download file with VB

If you want to ignore some errors, but throw others, you can use the HTTP Response status code to decide what to do:
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

You should not swallow errors - you should try to deal with it in some fashion even if it is just to log it.
You need to catch the WebException and then handle it by perhaps logging the error and then do not rethrow it.
Try
'Download file...
Catch ex As WebException
Logger.WriteError(ex)
End Try

Related

Occasional Exception Unhandled thrown although Break option is deactivated

I've set up my exceptions so that an error in the code
Try
Using Client As New WebClient
Client.DownloadFile(sExtract, sDownloadTo)
End Using
Catch ex As Exception
Debug.Print("Failed: " + sExtract)
End Try
isn't throw.
This works fine most of the time, but after like 50-100 of errors, the following exception is shown:
According to the checkbox state "Break when this exception type is thrown", which is not-activated, this exception shouldn't be shown this way, right?
What might cause this behaviour, and how could I change it so that this exception isn't thrown?
Here is an additional image of the QuickWatch:
Try
Dim Client As New WebClient
Client.DownloadFile(sExtract, sDownloadTo)
Catch ex As Exception
Console.writeline(ex.tostring)
End Try

How to Determine Exception Subtype

I'm wondering if there's a standard way to determine the sub-type of an exception. For example, for the File.Copy() method, IOException indicates that the destination file exists OR a general I/O error occurred. There are other such cases. In my exception handler, how can I determine which it is? I'm checking the end of ex.Message for the string already exists., which works, but seems awfully kludgy and unreliable.
While it is possible to check File.Exists() on the destination file, confirm overwrite with the user if it exists and then perform File.Copy() this is not atomic, which is to say, between checking and copying, it is possible for the conditions to change, for example if some other process created or copied a file into the destination location.
EDIT:
I had already changed the code based on comments here, but I just rolled it back and will post it here, just to show what I was doing:
Try
File.Copy(SrcFile, DstFile, OverWrite)
Catch ex As DirectoryNotFoundException
MsgBox(ex.Message)
Catch ex As FileNotFoundException
MsgBox("File not found: " & ex.FileName)
Catch ex As UnauthorizedAccessException
MsgBox("You do not have write access to the destination.")
Catch ex As IOException
' IOException represents an existing destination file OR a general IO error.
If SubStr(ex.Message, -15) = "already exists." Then
OverwriteCheck = MsgBox(
"Overwrite " & IO.Path.GetFileName(SrcFile) & " in destination directory?",
MsgBoxStyle.YesNo
)
If OverwriteCheck = DialogResult.Yes Then
Try
File.Copy(SrcFile, DstFile, OverWrite)
Catch iex As Exception
MsgBox("Unable to copy " & SrcFile & ":" & vbNewLine & iex.Message)
End Try
End If
Else
Throw ex
End If
Catch ex As ArgumentException
' The user left a blank line in the text box. Just skip it.
End Try
Here is an option using FileStreams to get more granular information about your exception
Sub Main()
Try
copyTo("C:\t\output3.txt", "C:\t\output1.txt", True)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
Private Sub copyTo(source As String, destination As String, Optional overwrite As Boolean = False)
' raises FileNotFoundException if source doesn't exist
Using fsSource As New FileStream(source, FileMode.Open, FileAccess.Read, FileShare.None)
If Not overwrite AndAlso File.Exists(destination) Then
' Raises exception when destination file exists and not overwrite
Throw New Exception(
String.Format("Destination file '{0}' exists and overwrite is false.", destination))
Else
Using fsDestination As New FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
fsSource.CopyTo(fsDestination)
End Using
End If
End Using
End Sub
This is a rudimentary example, but you can see how you can differentiate different exception cases, while having atomicity between checking file existence and copying.
I believe you looking for this pattern:
Try
IO.File.Copy("source", "Dest", True)
Catch exUnAuth As System.UnauthorizedAccessException
Catch exArg As System.ArgumentException
Catch exNotFound As IO.FileNotFoundException
Catch exGeneral As System.Exception
End Try
Place the list of specific exceptions first in the sequence. The last exception tested for should be the least derived.
You should read through the documentation: How to use structured exception handling in Visual Basic .NET or in Visual Basic 2005. Yes this is an old reference, but that is an indication how long this has been part of the language.

Unable to catch 404 error on HttpWebRequest response

Trying the below code to catch 404 error page but seems not to work, I mean 404 error doesn't display in response. The response only displays if the file exists (200 http code) !! but if the file doesn't exist (404) like in the below example, not msgbox displays, and the exception also doesn't display anything that's why I insert a msgbox in there.
If you try to take out these "111" from the "index.html" and run it, it'll display 200 http code well.
Using VS 2013, don't know, if it's a bug, or because I'm going through a proxy.
Will check it later from home for proxy as at work we're using proxy server.
Can anybody help !!
Try
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.cisco.com/c/en/us/support/index111.html")
Dim response As System.Net.HttpWebResponse = request.GetResponse
If response.StatusCode = Net.HttpStatusCode.OK Then
MsgBox("File exist on Server")
MsgBox(response.StatusCode) '200 HTTP CODE
'Trying to catch 404 error here
ElseIf response.StatusCode = Net.HttpStatusCode.NotFound Then
MsgBox("FILE NOT FOUND ON THE SERVER")
MsgBox(response.StatusCode)
Else
MsgBox("NO FILE on the server")
End If
Catch ex As Exception
'Msgbox("Error")
End Try
request.GetResponse will throw an exception when the request fails, for whatever reason.
There is a Response object in the exception through, on which you can ask what the status code was. Check ex.Response.StatusCode in your Catch block.

Selenium Not Catching Timeout Exception

I have just started with Selenium & quite like it so far but have come across an issue that is driving me insane! No matter what I do, I cannot seem to catch a "OpenQA.Selenium.WebDriverTimeoutException was unhandled by user code" exception.
The code I am using is:
Private Sub WaitForLoad(ByRef driver As PhantomJSDriver, ByVal Timeout As TimeSpan, ByVal FindField As IWebElement)
Try
Dim wait As WebDriverWait = New WebDriverWait(driver, Timeout)
wait.Until(ExpectedConditions.ElementToBeClickable(FindField))
Catch ex As OpenQA.Selenium.WebDriverTimeoutException
MessageBox.Show("1")
Throw New Exception(ex.Message)
Catch ex As WebDriverException
MessageBox.Show("2")
Throw New Exception(ex.Message)
Catch ex As System.TimeoutException
MessageBox.Show("3")
Throw New Exception(ex.Message)
Catch ex As Exception
MessageBox.Show("4")
Throw New Exception(ex.Message)
End Try
End Sub
I simply have the message boxes in there as I have been trying to catch all exception types to check which one would actually catch it but none will.
I am calling the function with, E.g.
WaitForLoad(driver, WaitTimeout, driver.FindElementByName("search"))
I have seen a few other questions similar to this on SO but all with the answer of "add WebDriverTimeoutException" which I have done with no luck as of yet. Hopefully somebody else has overcome this issue before I spend another few hours working out this issue!?!
After enabling "Enable Unmanaged Code Debugging" via Visual Studio this issue was resolved.

Check if folder on web exists or not

I'm creating desktop aplication and when I write a username in TextBox1 and on Button1.Click event it should check does folder on web exists.
As far I've tried this:
username = Me.TextBox1.Text
password = Me.TextBox2.Text
Dim dir As Boolean = IO.Directory.Exists("http://www.mywebsite.com/" + username)
If dir = true Then
Dim response As String = web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt")
If response.Contains(password) Then
MsgBox("You've logged in succesfully", MsgBoxStyle.Information)
Exit Sub
Else
MsgBox("Password is incorect!")
End If
Else
MsgBox("Username is wrong, try again!")
End If
First problem is that my boolean is giving FALSE as answer (directory exists for sure and all permissions are granted to see folder). I tried to solve that with setting dir = false and after that I go into first IF (but that's not what I want, since it should be TRUE, not FALSE)
There we come to second problem, in this line: Dim response As String=web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt") I get this error message: The remote server returned an error: (404) Not Found.
Anyone more experienced with this kind of things who can help me?
IO.Directory.Exists will not work in this case. That method only works to check for a folder on a disk somewhere (locally or network) ; you can't use it to check for the existence of a resource over HTTP. (i.e a URI)
But even if it did work this way, it's actually pointless to call it before attempting to download - the method DownloadString will throw an exception if something goes wrong - as you have seen, in this case it's telling you 404 Not Found which means "This resource does not exist as far as you are concerned". **
So you should try/catch the operation, you need to catch exceptions of type WebException, cast its Response member to HttpWebException, and check the StatusCode property.
An good example (albeit in C#) is here
** I say "as far as you are concerned" because for all you know, the resource may very well exist on the server, but it has decided to hide it from you because you do not have access to it, etc, and the developer of that site decided to return 404 in this case instead of 401 Unauthorised. The point being that from your point of view, the resource is not available.
Update:
here is the code from the answer I linked to, translated via this online tool because my VB is dodgy enough :). This code runs just fine for me in LinqPad, and produces the output "testlozinka"
Sub Main
Try
Dim myString As String
Using wc As New WebClient()
myString = wc.DownloadString("http://dota2world.org/HDS/Klijenti/TestKlijent/password.txt")
Console.WriteLine(myString)
End Using
Catch ex As WebException
Console.WriteLine(ex.ToString())
If ex.Status = WebExceptionStatus.ProtocolError AndAlso ex.Response IsNot Nothing Then
Dim resp = DirectCast(ex.Response, HttpWebResponse)
If resp.StatusCode = HttpStatusCode.NotFound Then
' HTTP 404
'the page was not found, continue with next in the for loop
Console.WriteLine("Page not found")
End If
End If
'throw any other exception - this should not occur
Throw
End Try
End Sub
Hope that helps.