检查 HTTP 位置中的空 csv 文件

Check for an empty csv file in HTTP location

我正在尝试按照 Determine if file is empty (SSIS) 查看 HTTP 位置的文件是否为空。我无法一开始就下载它,因为该过程在源代码处停滞不前,不会让我的包完成。我只想查询源文件,如果它有0条记录,退出进程并发送邮件。

我可以使用电子邮件部分,但我不确定如何从源头检查 0 条记录。我使用 vb.net 作为我的脚本语言。这是我目前所拥有的片段:

Public Sub Main()

        ' Get the unmanaged connection object, from the connection manager called "HTTP Connection Manager"
        Dim nativeObject As Object = Dts.Connections("HTTP_FileName").AcquireConnection(Nothing)

        ' Create a new HTTP client connection
        Dim connection As New HttpClientConnection(nativeObject)



        ' Download the file #1
        ' Save the file from the connection manager to the local path specified
        Dim filename As String = "DestinationPath"
        connection.DownloadFile(filename, True)

        Dts.TaskResult = ScriptResults.Success
    End Sub

编辑

如果不是 0 条记录,即使在 HTTP 位置检查 0kb 文件也应该可以达到目的。它可以在技术上检查文件大小,然后使脚本失败,从而引发适当的失败消息。

基于对问题的编辑:

如果只是检查响应的长度,您可以这样做:

If connection.DownloadData().Length = 0 Then

     'Logic for when there is no data.

End If

您可以获得原始字符串数据并以这种方式验证文件:

Dim strFileContents = System.Text.Encoding.UTF8.GetString(connection.DownloadData())

'Example file has header row but no content rows.  Your specific validation will be based 
'on what you're expecting from an empty csv file. 
If strFileContents Is Nothing OrElse  strFileContents.Split(System.Environment.NewLine).Count < 2 Then
     'Empty file.
End If

如果您使用的是 Microsoft .Net Framework 4,您可以使用 System.Net 库来实现:

您可以使用以下代码:

Imports System.Net

Public Sub Main()

    ' Get the unmanaged connection object, from the connection manager called "HTTP Connection Manager"
    Dim nativeObject As Object = Dts.Connections("HTTP_FileName").AcquireConnection(Nothing)

    ' Create a new HTTP client connection
    Dim connection As New HttpClientConnection(nativeObject)

    Dim webStream As IO.Stream
    Dim req As HttpWebRequest
    Dim res As HttpWebResponse
'Assuming that Dts.Connections("HTTP_FileName").ConnectionString Contains the file URL

    req = WebRequest.Create(Dts.Connections("HTTP_FileName").ConnectionString)

    req.Method = "GET" ' Method of sending HTTP Request(GET/POST)

    res = req.GetResponse() ' Send Request

    webStream = res.GetResponseStream() ' Get Response

    Dim webStreamReader As New IO.StreamReader(webStream)

    If webStreamReader.ReadToEnd.Length = 0 Then
          msgbox("File is Empty")
    Else
          Dim filename As String = "DestinationPath"
          connection.DownloadFile(filename, True)              
    End If

    Dts.TaskResult = ScriptResults.Success
End Sub

注意:@NoAlias 提供的方式也有效

If connection.DownloadData().Length = 0 Then

 'Logic for when there is no data.

End If