2011-02-28 91 views
0

我在2010 VB 初學者,我想編寫可下載文件VB 2010下載文件,並在404發生忽略錯誤

我的問題是一個應用程序:如果它給了我404或403或任何 ,我想,讓應用程序忽略了消息,而不是引發WebException錯誤

注:我已經知道如何下載文件,VB

回答

0

你不應該吞下錯誤 - 你應該嘗試在一些對付它即使只是記錄它時尚。

您需要捕獲WebException,然後通過記錄錯誤來處理它,然後再重新拋出它。

Try 
    'Download file... 
Catch ex As WebException 
    Logger.WriteError(ex) 
End Try 
2

如果你想忽略了一些錯誤,但扔別人,你可以使用HTTP響應狀態代碼,以決定該怎麼做:

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 
+0

在行「選擇案例Response.statuscode」它給我一個錯誤,對於遲來的反響抱歉:S – StillAzure 2011-03-19 22:00:45

+0

它給我「NullReferenceException」錯誤 – StillAzure 2011-03-19 22:04:48

+0

所以這意味着沒有響應。因此,在做出選擇案例之前,請檢查以確保迴應不是無關緊要的。 – 2011-03-19 23:28:46