如何从SSIS发出HTTP请求? [英] How to make an HTTP request from SSIS?

查看:206
本文介绍了如何从SSIS发出HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道如何从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.

推荐答案

您可以使用命名空间 System.Net.WebClient 在SSIS中借助脚本任务进行Http请求。以下示例显示了如何实现这一目标。该示例是在 SSIS 2008 R2 中创建的。

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.

分步流程:


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

  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.

在SSIS包上,放置脚本任务。使用脚本任务代码部分下提供的代码替换脚本任务中的Main()方法。请参阅屏幕截图# 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 显示路径 C:\ temp \ 为空。

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 显示代码已经过测试,可以下载.zip文件。为此,使用需要下载的内容网址更改了变量 RemoteUri 的值。

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.

脚本任务代码:

C#代码只能在中使用 SSIS 2008及以上

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

屏幕截图#1:

屏幕截图#2:

屏幕截图#3:

屏幕截图#4:

屏幕截图#5:

屏幕截图#6:

这篇关于如何从SSIS发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆