如何从SSIS发出HTTP请求?
我想知道如何从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
http://www.domain.com/resource.zip
并在驱动器上记录下载的日期时间和文件的目的地。我还想捕获文件大小等属性,并捕获下载完成的日期和时间。您可以使用名称空间System.Net.WebClient
在SSIS中的脚本任务
的帮助下发出Http请求。下面的示例显示了如何实现这一点。该示例是在SSIS 2008 R2中创建的
逐步过程:
http://www.google.com/intl/en_com/images/srpr/logo1w.png
。这是谷歌主页上徽标的图像url。使用值C:\temp\
设置变量LocalFolder
。这是我们要保存内容的路径。请参阅屏幕截图#1public 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;
}
脚本任务
。用脚本任务代码部分提供的代码替换脚本任务中的Main()方法。请参阅屏幕截图#2public 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;
}
C:\temp\
为空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:
以下是几个选项:
这只是@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身份验证您好,我们可以从需要用户和密码的网站下载一个文件吗?我有用户和密码,但我不知道如何将其传递到程序中?