检查HTTP位置中的空csv文件

时间:2017-02-13 16:56:41

标签: sql-server vb.net http ssis vb.net-2010

我正在尝试关注https://developers.google.com/web/tools/lighthouse/audits/first-meaningful-paint以查看HTTP位置的文件是否为空。我不能下载它开始,因为该过程停留在源头,不会让我的包完成。我想做的就是查询源文件,如果是0记录,退出流程并发送电子邮件。

我有电子邮件部分工作,但我不知道如何在源头检查0记录。我使用vb.net作为我的脚本语言。这是我到目前为止的一小部分:

if( isset($_POST['contryCode']) ){
   // query the db and have the result returned as json
   echo json_encode($result_query);
}

修改

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

2 个答案:

答案 0 :(得分:1)

如果您使用的是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

答案 1 :(得分:0)

基于编辑问题:

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

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