开发者

VB 2010 Download file and if 404 happen ignore error

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 t开发者_高级运维he 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜