Reputation: 2714
In SSIS I am using a VB.NET script task to download a file from an FTP folder.
The script is the following
Imports System
Imports System.Data
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Net
Public Class ScriptMain
Public Sub Main()
Dim objWebClient As WebClient = New WebClient()
Dim strDownloadURL As String = "ftp://mydownloadhosting.com/myfolder/" + Dts.Variables("GetDate").Value.ToString() + "_daily.xml"
Dim strFileName As String = Dts.Variables("WorkingFile").Value.ToString()
Dim wp As WebProxy = New WebProxy("my.proxy.local", 1234)
objWebClient.Proxy = wp
objWebClient.Credentials = New System.Net.NetworkCredential("username", "password")
objWebClient.DownloadFile(strDownloadURL, strFileName)
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
it works correctly but my target is to manage the exception, in particular to discriminate between:
I have made some research about how to manage exception with WebClient()
and I have found these:
which they give different forms of the following:
try
{
// try to download file here
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
{
// handle the 404 here
}
}
else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
{
// handle name resolution failure
}
}
The main problem is that my code is in VB.NET and all the posted answered are written in C#, how can make a try
/catch
construct to handle an exception in my code?
Upvotes: 2
Views: 761
Reputation: 202504
An equivalent code in VB.NET is:
Try
' try to download file here
Catch ex As WebException
If ex.Status = WebExceptionStatus.ProtocolError Then
If DirectCast(ex.Response, HttpWebResponse).StatusCode = HttpStatusCode.NotFound Then
' // handle the 404 here
End If
ElseIf ex.Status = WebExceptionStatus.NameResolutionFailure Then
' handle name resolution failure
End If
End Try
Though the above/your code is for HTTP, not for FTP. FTP has different status codes.
For FTP, use:
For some FTP examples, see:
Upvotes: 2
Reputation: 37348
There are many C# to VB.NET converters that you can refer to when you need to convert simple codes:
The equivalent VB.NET code is:
Imports System
Imports System.Data
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Net
Public Class ScriptMain
Public Sub Main()
Try
Dim objWebClient As WebClient = New WebClient()
Dim strDownloadURL As String = "ftp://mydownloadhosting.com/myfolder/" + Dts.Variables("GetDate").Value.ToString() + "_daily.xml"
Dim strFileName As String = Dts.Variables("WorkingFile").Value.ToString()
Dim wp As WebProxy = New WebProxy("my.proxy.local", 1234)
objWebClient.Proxy = wp
objWebClient.Credentials = New System.Net.NetworkCredential("username", "password")
objWebClient.DownloadFile(strDownloadURL, strFileName)
Dts.TaskResult = Dts.Results.Success
Catch ex As WebException
If ex.Status = WebExceptionStatus.ProtocolError Then
If (CType(ex.Response, HttpWebResponse)).StatusCode = HttpStatusCode.NotFound Then
'handle the 404 here
End If
ElseIf ex.Status = WebExceptionStatus.NameResolutionFailure Then
'handle name resolution failure
End If
Dts.TaskResult = Dts.Results.Failure
End Try
End Sub
End Class
Upvotes: 0