【问题标题】:How to make an HTTP request from SSIS?如何从 SSIS 发出 HTTP 请求?
【发布时间】:2011-10-04 18:53:45
【问题描述】:

我很想知道如何从 SSIS 发起 HTTP 调用。例如,我希望能够从http://www.domain.com/resource.zip 下载文件并记录下载的日期时间和驱动器上文件的目的地。我还想捕获文件大小等属性并捕获下载完成的日期和时间。

【问题讨论】:

  • 这个答案很棒,但是我如何从下载的 Uri 中获取文件大小之类的东西?完成后,我需要使用文件大小信息更新源表
  • 我不明白这不是 ETL 工具内置的...

标签: http ssis


【解决方案1】:

您可以使用命名空间System.Net.WebClient 在SSIS 中借助Script Task 发出Http 请求。以下示例显示了如何实现这一点。该示例创建于SSIS 2008 R2

分步过程:

  1. 创建一个新的 SSIS 包并创建两个变量,即 RemoteUriLocalFolder。将变量RemoteUri 设置为值http://www.google.com/intl/en_com/images/srpr/logo1w.png。这是 Google 主页上徽标的图片网址。将变量LocalFolder 设置为值C:\temp\。这是我们要保存内容的路径。请参阅屏幕截图 #1

  2. 在 SSIS 包上,放置 Script Task。将脚本任务中的 Main() 方法替换为 脚本任务代码 部分下提供的代码。请参阅屏幕截图 #2

  3. 截图#3显示路径C:\temp\为空。

  4. 屏幕截图 #4 显示包已成功执行。

  5. 屏幕截图 #5 显示内容(在本例中为徽标图像)已下载到本地文件夹路径。

  6. 屏幕截图 #6 显示已测试代码以下载 .zip 文件。为此,变量 RemoteUri 的值被更改为需要下载的内容 url。

脚本任务代码:

C# 代码只能在 SSIS 2008 and above 中使用。

public void Main()
{
    Variables varCollection = null;

    Dts.VariableDispenser.LockForRead("User::RemoteUri");
    Dts.VariableDispenser.LockForRead("User::LocalFolder");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    System.Net.WebClient myWebClient = new System.Net.WebClient();
    string webResource = varCollection["User::RemoteUri"].Value.ToString();
    string fileName = varCollection["User::LocalFolder"].Value.ToString() + webResource.Substring(webResource.LastIndexOf('/') + 1);
    myWebClient.DownloadFile(webResource, fileName);

    Dts.TaskResult = (int)ScriptResults.Success;
}

屏幕截图 #1:

屏幕截图 #2:

截图#3:

屏幕截图 #4:

屏幕截图 #5:

屏幕截图 #6:

【讨论】:

  • 这很有帮助,但我想更好地控制我的 HTTP 请求——例如POST 带有特定的请求正文,System.Net.WebClient 显然不能这样做。
  • 你好,请问我们可以从需要用户名和密码的网站下载文件吗?我有用户名和密码,但我不知道如何将其传递到程序中?
【解决方案2】:

只是@user756519 脚本的替代品,速度不快,但更防弹

public void Main()
{
    Variables varCollection = null;

    Dts.VariableDispenser.LockForRead("User::RemoteUri");
    Dts.VariableDispenser.LockForRead("User::LocalFolder");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    System.Net.WebClient myWebClient = new System.Net.WebClient();
    string webResource = varCollection["User::RemoteUri"].Value.ToString();
    string fileName = varCollection["User::LocalFolder"].Value.ToString() + webResource.Substring(webResource.LastIndexOf('/') + 1);

    byte[] data;
    using (WebClient client = new WebClient())
    {
        data = client.DownloadData(webResource);
    }
    FileInfo file = new System.IO.FileInfo(fileName);
    file.Directory.Create(); // If the directory already exists, this method does nothing.
    File.WriteAllBytes(file.FullName, data);

    Dts.TaskResult = (int)ScriptResults.Success;
}

这样,webClient 不会一直挂起,而且您也不依赖于先前存在的 C:\Temp 目录。 除此之外,@user756519 的回答很好,非常详细。

【讨论】:

  • 你好,请问我们可以从需要用户名和密码的网站下载文件吗?我有用户名和密码,但我不知道如何将其传递到程序中?
【解决方案3】:

这里有几个选项:

  1. 第三方工具,例如 CozyRoc 或 BlueSSIS。
  2. 使用 WebClient 编写任务脚本
  3. 使用 HTTP 连接管理器编写任务

脚本任务示例位于: http://microsoft-ssis.blogspot.com/2011/05/download-source-file-from-website-with.html

【讨论】:

  • 我认为这是 HTTP 连接管理器的目的,但即使您将其列为第三选择。为什么这不是首选选项?
  • HTTP 连接管理器的缺点是它不支持 WIndows 身份验证
猜你喜欢
  • 2021-07-20
  • 2017-03-09
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多