如何从 SSIS 发出 HTTP 请求?

发布于 2024-11-19 19:07:37 字数 147 浏览 1 评论 0原文

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

I'm interested in knowing how I can make an HTTP call from SSIS. For example, I would like to be able to download a file from http://www.domain.com/resource.zip and record the datetime of the download and the destination of the file on the drive. I would also like to capture such attributes as file size and capture the date & time when the download completed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-11-26 19:07:37

您可以利用命名空间 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 主页上徽标的图像 URL。将变量 LocalFolder 设置为值 C:\temp\。这是我们要保存内容的路径。请参阅屏幕截图#1

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

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

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

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

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

脚本任务代码:

C#代码,只能在SSIS 2008及更高版本中使用。

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:

1

屏幕截图 #2:

2

屏幕截图#3:

3

屏幕截图 #4:

4

屏幕截图 #5:

5

屏幕截图 #6:

You can make use of the namespace System.Net.WebClient to make the Http request with the help of Script Task in SSIS. Following example shows how this can be achieved. The example was created in SSIS 2008 R2.

Step-by-step process:

  1. Create a new SSIS package and create two variables namely RemoteUri and LocalFolder. Set the variable RemoteUri with the value http://www.google.com/intl/en_com/images/srpr/logo1w.png. this is the image url of the logo on the Google's home page. Set the variable LocalFolder with the value C:\temp\. this is the path where we are going to save the content. Refer screenshot #1.

  2. On the SSIS package, place a Script Task. Replace the Main() method within the script task with the code provided under the Script Task Code section. Refer screenshot #2.

  3. Screenshot #3 shows that the path C:\temp\ is empty.

  4. Screenshot #4 shows successful execution of the package.

  5. Screenshot #5 shows that the content (in this case the logo image) has been downloaded to the local folder path.

  6. Screenshot #6 shows that the code was tested to download a .zip file. To achieve this, the value of the variable RemoteUri was changed with the content url that needs to be downloaded.

Script task code:

C# code that can be used only in 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;
}

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

盛装女皇 2024-11-26 19:07:37

只是 @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 的回答很好,非常详细。

Just an alternative for @user756519 script, not as fast, but more bulletproof

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;
}

This way, webClient doesn't stay hanging, and also you're not dependent on the previous existence of C:\Temp directory.
Other than that, great answer from @user756519, very detailed.

野味少女 2024-11-26 19:07:37

以下是几个选项:

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

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

Here are a couple of options:

  1. Third party tools such as CozyRoc or BlueSSIS.
  2. Script Task with WebClient
  3. Script Task with HTTP Connection Manager

Script Task Examples at:
http://microsoft-ssis.blogspot.com/2011/05/download-source-file-from-website-with.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文