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

标签 vb.net exception ssis ftp webclient

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 中的等效代码是:

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

关于vb.net - 使用 WebClient 处理 VB.NET SSIS 脚本中的异常(FTP 下载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55257079/

相关文章:

java - 是否应该在 throws 子句中声明 IllegalArgumentException?

VB.NET Winforms : Overlay two transparent images

java - webMethods 获取 ServiceException 的根本原因

python - 如果字典有多个值,如何使用 try except ?

oracle - SSIS 2008 Excel 目标连接错误 - 0x80040E37

asp.net - 将消息从 SSIS (2012) 包推送到 ASP.NET Web 应用程序中的 SignalR 中心 - 最好的方法是什么?

c# - 在 Web 浏览器控件中控制 Excel

vb.net - 属性定义中括号的含义是什么?

asp.net - Visual Studio 2015 中的 "Visual Basic 10.0 does not support readonly auto-implemented properties"错误

sql - 对 ETL 的良好 SQL Server Integration Services (SSIS) 示例/样本的建议?