Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从SSIS发出HTTP请求?_Http_Ssis - Fatal编程技术网

如何从SSIS发出HTTP请求?

如何从SSIS发出HTTP请求?,http,ssis,Http,Ssis,我想知道如何从SSIS进行HTTP调用。例如,我希望能够从http://www.domain.com/resource.zip并在驱动器上记录下载的日期时间和文件的目的地。我还想捕获文件大小等属性,并捕获下载完成的日期和时间。您可以使用名称空间System.Net.WebClient在SSIS中的脚本任务的帮助下发出Http请求。下面的示例显示了如何实现这一点。该示例是在SSIS 2008 R2中创建的 逐步过程: 创建一个新的SSIS包并创建两个变量,即RemoteUri和LocalFolde

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

您可以使用名称空间
System.Net.WebClient
在SSIS中的
脚本任务
的帮助下发出Http请求。下面的示例显示了如何实现这一点。该示例是在SSIS 2008 R2中创建的

逐步过程:

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

  • 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;
    }
    
  • 在SSIS包上,放置一个
    脚本任务
    。用脚本任务代码部分提供的代码替换脚本任务中的Main()方法。请参阅屏幕截图#2

  • 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;
    }
    
  • 屏幕截图#3显示路径
    C:\temp\
    为空

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

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

  • 屏幕截图#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:

    截图#2:

    屏幕截图#3:

    屏幕截图#4:

    屏幕截图#5:

    屏幕截图#6:


    以下是几个选项:

  • 第三方工具,如CozyRoc或Bluesis
  • 使用WebClient编写任务脚本
  • 使用HTTP连接管理器编写脚本任务
  • 脚本任务示例位于:

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

    这个回答很好,但是如何从下载的Uri中获得文件大小之类的内容呢?一旦完成,我需要用文件大小信息更新sources表我不明白这不是ETL工具内置的…这很有用,但我希望对HTTP请求有更多的控制--例如,使用特定的请求体发布,哪个System.Net.WebClient显然不能。您好,我们可以从需要用户和密码的网站下载一个文件,以提供我有用户和密码,但我不知道如何将其传递到程序中?我以为这是HTTP连接管理器的用途,但即使您将其列为第三个选择。为什么这不是“转到”选项?HTTP连接管理器的缺点是它不支持WIndows身份验证您好,我们可以从需要用户和密码的网站下载一个文件吗?我有用户和密码,但我不知道如何将其传递到程序中?