【问题标题】:Handle exceptions in VB.NET SSIS script with WebClient (FTP download)使用 WebClient 处理 VB.NET SSIS 脚本中的异常(FTP 下载)
【发布时间】:2019-08-10 22:18:31
【问题描述】:

在 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


    【解决方案1】:

    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 示例,请参阅:

    【讨论】:

      【解决方案2】:

      有很多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
      

      【讨论】:

      • 将代码与ftp:// 和HttpWebResponse 组合在一起的方式永远不会奏效。 ex.Response 到 HttpWebResponse 的演员阵容将失败。
      • @MartinPrikryl 我没有检查代码,因为 OP 提到代码工作正常。但是为什么铸造会失败?
      • 因为对于 FTP,响应的类型是 FtpWebResponse。看我的回答。 -- 我不认为 OP 曾经测试过 C# 代码。 OP 只是错误地假设代码是正确的。
      • @MartinPrikryl 哎呀我没有提到。你是对的,正如我之前提到的,我只是尝试提供完整的等效 VB.NET 代码而不检查功能。
      • @MartinPrikryl 因为目标是帮助 OP 我也支持你的。谢谢
      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多