如何从SSIS发出HTTP请求?

tap*_*tap 31 ssis http

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

小智 32

您可以在SSIS System.Net.WebClient的帮助下利用命名空间来发出Http请求Script Task.以下示例显示了如何实现这一目标.这个例子是在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文件.为此 ,使用需要下载的内容URL更改了变量RemoteUri的值 .

脚本任务代码:

只能使用的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;
}
Run Code Online (Sandbox Code Playgroud)

截图#1:

1

截图#2:

2

截图#3:

3

截图#4:

4

截图#5:

五

截图#6:

6


RMi*_*nda 7

只是@ 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;
}
Run Code Online (Sandbox Code Playgroud)

这样,webClient就不会挂起,而且你也不依赖于之前存在的C:\ Temp目录.除此之外,@ user756519的答案很棒,非常详细.


Joo*_*ost 5

这里有几个选项:

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

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