使用WebClient处理VB.NET SSIS脚本中的异常(FTP下载)

问题描述 投票:2回答:2

在SSIS中,我使用VB.NET脚本任务从FTP文件夹下载文件。

该脚本如下

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

它工作正常,但我的目标是管理异常,特别是区分:

  • 文件未找到
  • 所有其他问题(超时,代理问题,......)

我已经对如何使用WebClient()管理异常进行了一些研究,我发现了这些:

他们给出了以下不同形式:

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
    }
}

主要问题是我的代码是在VB.NET中,并且所有发布的答案都是用C#编写的,如何使用try / catch构造来处理我的代码中的异常?

vb.net exception ssis ftp webclient
2个回答
0
投票

当您需要转换简单代码时,可以参考许多C#到VB.NET转换器:

等效的VB.NET代码是:

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

2
投票

VB.NET中的等效代码是:

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

虽然上面/你的代码是针对HTTP而不是针对FTP。 FTP具有不同的状态代码。

对于FTP,请使用:

有些FTP示例,请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.