如何从 SSIS 发出 HTTP 请求? [英] How to make an HTTP request from SSIS?
问题描述
我很想知道如何从 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
在 Script Task 的帮助下进行 Http 请求
在 SSIS 中.以下示例显示了如何实现这一点.该示例是在 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
.
分步过程:
创建一个新的 SSIS 包并创建两个变量,即 RemoteUri 和 LocalFolder.使用值
http://www.google.com/intl/en_com/images/srpr/logo1w.png
设置变量RemoteUri
.这是 Google 主页上徽标的图像 URL.使用值C: emp
设置变量LocalFolder
.这是我们要保存内容的路径.请参阅屏幕截图 #1.
Create a new SSIS package and create two variables namely RemoteUri and LocalFolder. Set the variable
RemoteUri
with the valuehttp://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 variableLocalFolder
with the valueC: emp
. this is the path where we are going to save the content. Refer screenshot #1.
在 SSIS 包上,放置一个 Script Task
.将脚本任务中的 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: emp
为空.
Screenshot #3 shows that the path C: emp
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 的值被更改为需要下载的内容 url.
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屋!